otel only.Expand description
Reusable chat / execute_tool span + metric helpers.
These primitives are shared by every agent-loop implementation so the daemon-hosted worker emits byte-identical telemetry to the in-process loop.
§Why this module exists
The SDK ships two agent-loop implementations:
- the in-process
agent_loop(used byAgentLoop/BipAgent), and - the daemon-hosted re-implementation in
agent-server’s worker (crates/agent-server/src/worker/root_turn.rs+agent-service-host’sregistry_tool_executor.rs).
Both drive an LLM call and a tool dispatch, and both must emit the
same gen_ai.* / agent_sdk.* telemetry so dashboards built
against the in-process loop light up unchanged when a session runs
on the daemon. Before this module the worker bypassed the SDK
instrumentation entirely and emitted none of it.
Rather than copy the span names, attribute keys, and metric label
sets into agent-server (where they would silently drift), this
module exposes the exact primitives the in-process loop uses:
build_chat_span/finish_chat_span_success/finish_chat_span_errormirroragent_loop::turn’sbuild_llm_span/stamp_llm_success/stamp_llm_error.build_tool_span/finish_tool_span/ToolSpanOutcomemirroragent_loop::tool_execution’sstart_tool_span/finish_tool_span.
The metric recording underneath delegates to
crate::observability::metrics::Metrics (the same global
singleton), so there is a single source of truth for the label
sets and no second meter scope.
Compiled only with feature = "otel".
Structs§
- Chat
Span Params - Inputs needed to open a
chat {model}CLIENT span. - Root
Turn Span Params - Inputs needed to open the daemon’s root
invoke_agentspan. - Started
Root Turn Span - A started root-turn span plus the hex ids the worker persists so the turn’s later tasks can re-parent their spans under it.
- Tool
Span Params - Inputs needed to open an
execute_toolINTERNAL span.
Enums§
- Tool
Span Outcome - Terminal outcome of a tool execution, used to stamp the outcome
attributes + metric labels on an
execute_toolspan.
Functions§
- build_
chat_ span - Open a
chat {model}CLIENT span with thegen_aisemconv attributes known before the call. - build_
tool_ span - Open an
execute_toolINTERNAL span. - classify_
llm_ error - Map a free-form LLM error message to the stable
error.typeattribute / metric label. - context_
from_ traceparent - Rebuild an
OTelContextfrom a persisted W3Ctraceparent. - discard_
root_ turn_ span - Drop the stashed root-turn span for
task_idwithout finalizing. - finalize_
root_ turn_ span - Finalize and end the stashed root-turn span for
task_id, stamping run-outcome + usage attributes (seefinish_root_turn_span). - finish_
chat_ span_ error - Stamp a chat error onto
span, record thegen_ai.client.operation.durationmetric with theerror.typelabel, and end the span. - finish_
chat_ span_ success - Stamp a successful chat response onto
span, record thegen_ai.client.token.usage+gen_ai.client.operation.durationmetrics, and end the span. - finish_
reason_ str - Map an SDK
StopReasonto the semconvfinish_reasonstring. - finish_
root_ turn_ span - Finalize the root-turn span with run-outcome attributes + the
agent_sdk.runs.outcomecounter, then end it. - finish_
tool_ span - Stamp the terminal outcome on
span, record theagent_sdk.tools.execution.{count,duration}metrics, and end the span. - remote_
parent_ context - Build a
Contextwhose active parent is the root-turn span identified by the hex ids persisted on the turn attempt. - remote_
parent_ context_ with_ sampling - Re-parent a child onto a remote span, preserving the root’s real sampled bit.
- start_
root_ turn_ span - Start the daemon’s root
invoke_agentINTERNAL span and capture the ids the worker persists so the turn’s later tasks can nest under it. - stash_
root_ turn_ span - Stash the live root-turn
spanundertask_idso a later task can finalize it with the correct full duration (seeLIVE_ROOT_SPANS). - traceparent_
from_ ids - Encode hex trace + span ids into a W3C
traceparentstring. - traceparent_
from_ ids_ with_ sampling - Encode hex trace + span ids into a W3C
traceparent, stamping the root span’s real sampled bit in the flag byte (-01when sampled,-00otherwise).