@trace decorator for wrapping agent functions, and the traced_run context manager for scripts, notebooks, or dynamic workflows where a decorator doesn’t fit. Both create an active run context that record_llm_call, record_tool_call, and record_state automatically attach to.
@trace decorator
Apply @trace to any function to turn it into a traced run. When the function is called, AgentDbg emits a RUN_START event, runs the body, then emits RUN_END. If the function raises, AgentDbg emits ERROR followed by RUN_END with status="error" and re-raises the exception.
Parameters
Run name shown in
agentdbg list and the timeline UI. Defaults to the function name when not set.Abort the run when loop detection emits a
LOOP_WARNING event. Raises AgentDbgLoopAbort.Minimum number of repeated pattern occurrences required before
stop_on_loop triggers.Abort after more than N LLM calls. Raises
AgentDbgGuardrailExceeded.Abort after more than N tool calls. Raises
AgentDbgGuardrailExceeded.Abort after more than N total events recorded. Raises
AgentDbgGuardrailExceeded.Abort when elapsed run time reaches this limit in seconds. Raises
AgentDbgGuardrailExceeded.traced_run context manager
traced_run starts a traced run using a with block. Use it when you cannot or don’t want to use a decorator — for example, in a Jupyter notebook cell, a script with a flat structure, or a dynamic workflow that assembles steps at runtime.
traced_run as well:
traced_run accepts the same parameters as @trace. See the parameter table above.
traced_run behavior is identical to @trace: if no run is already active it creates a new one; if a run is already active it attaches to it without creating nested run events.has_active_run
has_active_run() returns True when a traced run is active in the current context — that is, inside a @trace-decorated function call or an open traced_run block.
has_active_run when you are writing utility code or library integrations that should record events only when the caller has already started a run, rather than creating a new implicit one.
Nested runs
Calling a@trace-decorated function from inside an already active run does not create a nested run or extra RUN_START/RUN_END events. All record_* calls inside the inner function still attach to the outer run.
@trace to helper functions without worrying about polluting the timeline with spurious run boundaries.
Implicit runs
By default, callingrecord_llm_call, record_tool_call, or record_state outside any traced run does nothing — the call is silently ignored.
If your script does not have a single top-level entrypoint you can wrap, set the environment variable: