Skip to content

Tracing

A trace that can be exported to JSON or printed to the console.

Main trace object containing execution spans and final output.

FieldTypeDescription
spanslist[AgentSpan]A list of [AgentSpan][AgentSpan] that form the trace.
final_outputstr | dict[str, Any] | BaseModel | NoneContains the final output message returned by the agent.
  • duration - timedelta: Duration of the agent invocation span.
  • tokens - TokenInfo: Total token usage across all LLM calls (cached).
  • cost - CostInfo: Total cost across all LLM calls (cached).

Add an AgentSpan to the trace and clear the tokens_and_cost cache if present.

def add_span(
self,
span: AgentSpan | Span,
) -> None

Add a list of AgentSpans to the trace and clear the tokens_and_cost cache if present.

def add_spans(
self,
spans: list[AgentSpan],
) -> None

Convert spans to standard message format.

def spans_to_messages(
self,
) -> list[AgentMessage]

Serializable representation of an OpenTelemetry span.

FieldTypeDescription
namestr
kindSpanKind
parentSpanContext | None
start_timeint | None
end_timeint | None
statusStatus
contextSpanContext
attributesdict[str, Any]
linkslist[Link]
eventslist[Event]
resourceResource

Check whether this span is an agent invocation (the very first span).

def is_agent_invocation(
self,
) -> bool

Check whether this span is a call to an LLM.

def is_llm_call(
self,
) -> bool

Check whether this span is an execution of a tool.

def is_tool_execution(
self,
) -> bool

Extract input messages from an LLM call span.

def get_input_messages(
self,
) -> list[AgentMessage] | None

Extract output content from an LLM call or tool execution span.

def get_output_content(
self,
) -> str | None

FieldTypeDescription
input_costfloat
output_costfloatCost associated to the output tokens.

Properties: total_cost - float: Total cost (input + output).


FieldTypeDescription
input_tokensintNumber of input tokens.
output_tokensintNumber of output tokens.

Properties: total_tokens - int: Total tokens (input + output).


Constants for accessing span attributes following OpenTelemetry semantic conventions for generative AI systems.

AttributeDescription
GenAI.AGENT_DESCRIPTIONgen_ai.agent.description
GenAI.AGENT_NAMEgen_ai.agent.name
GenAI.INPUT_MESSAGESgen_ai.input.messages
GenAI.OPERATION_NAMEgen_ai.operation.name
GenAI.OUTPUTgen_ai.output
GenAI.OUTPUT_TYPEgen_ai.output.type
GenAI.REQUEST_MODELgen_ai.request.model
GenAI.TOOL_ARGSgen_ai.tool.args
GenAI.TOOL_DESCRIPTIONgen_ai.tool.description
GenAI.TOOL_NAMEgen_ai.tool.name
GenAI.USAGE_INPUT_COSTgen_ai.usage.input_cost
GenAI.USAGE_INPUT_TOKENSgen_ai.usage.input_tokens
GenAI.USAGE_OUTPUT_COSTgen_ai.usage.output_cost
GenAI.USAGE_OUTPUT_TOKENSgen_ai.usage.output_tokens