Choose a setup method
Start with one method for a call path. Combining SDK tracking with framework
OpenTelemetry on the same call can create duplicate interactions.
Recommended: Agnost AI skill
Recommended. Choose this path when you want your coding agent to inspect the
project, select a supported transport, make the smallest instrumentation change,
send a test event, and verify that it reached Agnost AI.
Manual setup: Agnost AI SDK
agnost.shutdown() once when the application exits, not after each request.
Shutting down per request disables telemetry for later agent runs in the same process.
Manual setup: OpenTelemetry
AG2 1.x shipsTelemetryMiddleware, which emits OpenTelemetry spans that follow the GenAI semantic conventions: one invoke_agent span per turn, one chat span per model call, and one execute_tool span per tool call. Agnost AI ingests those spans directly. Send them over OTLP, stamp the session and user on them, and wrap each turn in one short span that carries the user’s message and the agent’s answer.
1. Install
ag2 1.3.4, anthropic 2.14.0, and opentelemetry-sdk 1.45.2 on Python 3.12.
2. Point OpenTelemetry at Agnost AI and run an agent
TelemetryMiddlewaretraces the agent turn, each model call, and each tool call. Content capture is on by default, so model spans carrygen_ai.input.messagesandgen_ai.output.messages, and tool spans carrygen_ai.tool.call.argumentsandgen_ai.tool.call.result.span_attributesstampsgen_ai.conversation.idanduser.idonto every span the middleware emits. Every turn with the samesession_idlands in the same Agnost AI conversation. Because the values change per request, the middleware is created per turn and passed toagent.ask(..., middleware=[...])rather than to theAgentconstructor.- The
agent_turnspan becomes the root of each trace. Agnost AI builds the conversation transcript from each trace’s root span, and AG2’sinvoke_agentspan carries no message text. The wrapper puts the user’s message and the final answer there. open_telemetry=Falsestops theanthropicSDK from adding its ownchatspan under every AG2chatspan, so each model call is recorded once.
TracerProvider once at startup, call run_turn per request, and call provider.shutdown() on graceful shutdown.
What appears in Agnost AI
- Conversations: one per
session_id. Agnost AI keeps the raw session ID asoriginal_conversation_idin the conversation metadata. - Users: the
user_idyou pass tospan_attributes. - Messages: the user’s message and the agent’s answer for each turn, from the
agent_turnspan. - Events: the
support-agentturn (agent), each model call (llm) with input and output messages and token counts, and each tool call (tool) with the tool name, arguments, and result. AG2 also emits arecord_usagespan per model call with the same token counts.
Verify
Run the script, wait up to a minute for ingestion, then open Conversations in Agnost AI. Confirm the conversation shows youruser_id, a user message and an assistant answer, and a get_order_status tool event with arguments and a result.
Troubleshooting
- Nothing arrives: confirm the exporter’s headers contain
X-Agnost-Org-ID, and that the script callsprovider.shutdown()orprovider.force_flush()before it exits.BatchSpanProcessorexports in the background. TelemetryMiddlewareimport fails: install thetracingextra,pip install "ag2[tracing]".- Events arrive but the conversation has no messages: the turn ran outside the
agent_turnspan. Callagent.ask()inside it. - Every turn is a separate conversation, or the user is missing: pass
span_attributeswithgen_ai.conversation.idanduser.idon each turn. - Each model call appears twice: set
open_telemetry=FalseonAnthropicConfig, or setANTHROPIC_OPEN_TELEMETRY=false. - Model and tool events have no text:
capture_contentwas set toFalseonTelemetryMiddleware. - You use AG2 0.x (
from autogen import ConversableAgent):TelemetryMiddlewareis part of the AG2 1.x API. Upgrade toag21.x, or use the Agnost AI SDK setup above.
References
Next steps
- Conversations: review the complete AG2 session.
- Events: inspect agent, model, and tool activity.
- Patterns: organize production conversations by what users wanted.