Run metadata
Attach a small amount of data to a run and update it as the run progresses.
You can attach up to 4KB (4,096 bytes) of metadata to a run, which you can then access from inside the run function, via the API, and in the dashboard. You can use metadata to store additional, structured information on a run. For example, you could store your user’s full name and corresponding unique identifier from your system on every task that is associated with that user.
Usage
Add metadata to a run by passing it as an object to the trigger
function:
You can get the current metadata at any time by calling metadata.get()
or metadata.current()
(only inside a run):
Any of these methods can be called anywhere “inside” the run function, or a function called from the run function:
If you call any of the metadata methods outside of the run function, they will have no effect:
This means it’s safe to call these methods anywhere in your code, and they will only have an effect when called inside the run function.
Calling metadata.current()
or metadata.get()
outside of the run function will always return
undefined.
These methods also work inside any task lifecycle hook, either attached to the specific task or the global hooks defined in your trigger.config.ts
file.
Updates API
One of the more powerful features of metadata is the ability to update it as the run progresses. This is useful for tracking the progress of a run, storing intermediate results, or storing any other information that changes over time. (Combining metadata with Realtime can give you a live view of the progress of your runs.)
All metadata update methods (accept for flush
and stream
) are synchronous and will not block the run function. We periodically flush metadata to the database in the background, so you can safely update the metadata inside a run as often as you need to, without worrying about impacting the run’s performance.
set
Set the value of a key in the metadata object:
del
Delete a key from the metadata object:
replace
Replace the entire metadata object with a new object:
append
Append a value to an array in the metadata object:
remove
Remove a value from an array in the metadata object:
increment
Increment a numeric value in the metadata object:
decrement
Decrement a numeric value in the metadata object:
stream
Capture a stream of values and make the stream available when using Realtime. See our Realtime streams documentation for more information.
metadata.stream
accepts any AsyncIterable
or ReadableStream
object. The stream will be captured and made available in the Realtime API. So for example, you could pass the body of a fetch response to metadata.stream
to capture the response body and make it available in Realtime:
Or the results of a streaming call to the OpenAI SDK:
flush
Flush the metadata to the database. The SDK will automatically flush the metadata periodically, so you don’t need to call this method unless you need to ensure that the metadata is persisted immediately.
Metadata propagation
Metadata is NOT propagated to child tasks. If you want to pass metadata to a child task, you must do so explicitly:
Type-safe metadata
The metadata APIs are currently loosely typed, accepting any object that is JSON-serializable:
If you pass in an object like a Date, it will be serialized to a string when stored in the
metadata. That also means that when you retrieve it using metadata.get()
or
metadata.current()
, you will get a string back. You will need to deserialize it back to a Date
object if you need to use it as a Date.
We recommend wrapping the metadata API in a Zod schema (or your validator library of choice) to provide type safety:
Inspecting metadata
Dashboard
You can view the metadata for a run in the Trigger.dev dashboard. The metadata will be displayed in the run details view:
API
You can use the runs.retrieve()
SDK function to get the metadata for a run:
See the API reference for more information.
Size limit
The maximum size of the metadata object is 4KB. If you exceed this limit, the SDK will throw an error. If you are self-hosting Trigger.dev, you can increase this limit by setting the TASK_RUN_METADATA_MAXIMUM_SIZE
environment variable. For example, to increase the limit to 16KB, you would set TASK_RUN_METADATA_MAXIMUM_SIZE=16384
.