wrapAgent.
Required
Create a run wrapper
Execute the run
runId is the stable identifier you can store, return to clients, and correlate with downstream events.
Optional run data
Run-level data is usually attached as span attributes from inside the wrapped function:lemma.user_idlemma.session_idlemma.environmentlemma.feature
Mark a run as failed
UserecordError and rethrow so the run is visibly errored and your app still handles failures normally.
Dashboard outcome
A run appears as the top-levelai.agent.run span with:
- total duration
- final output from
onComplete - error state (if
recordErroris called or an uncaught error occurs) - custom attributes for filtering

