Skip to main content

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. 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.
Install the skill once in your coding-agent environment:
Run the framework-specific prompt below from the application root. Review the resulting diff before deploying it.

Manual setup: Agnost AI SDK

Call 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 ships TelemetryMiddleware, 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

Tested with 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

What each piece does:
  • TelemetryMiddleware traces the agent turn, each model call, and each tool call. Content capture is on by default, so model spans carry gen_ai.input.messages and gen_ai.output.messages, and tool spans carry gen_ai.tool.call.arguments and gen_ai.tool.call.result.
  • span_attributes stamps gen_ai.conversation.id and user.id onto every span the middleware emits. Every turn with the same session_id lands in the same Agnost AI conversation. Because the values change per request, the middleware is created per turn and passed to agent.ask(..., middleware=[...]) rather than to the Agent constructor.
  • The agent_turn span becomes the root of each trace. Agnost AI builds the conversation transcript from each trace’s root span, and AG2’s invoke_agent span carries no message text. The wrapper puts the user’s message and the final answer there.
  • open_telemetry=False stops the anthropic SDK from adding its own chat span under every AG2 chat span, so each model call is recorded once.
In a long-running server, create the 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 as original_conversation_id in the conversation metadata.
  • Users: the user_id you pass to span_attributes.
  • Messages: the user’s message and the agent’s answer for each turn, from the agent_turn span.
  • Events: the support-agent turn (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 a record_usage span 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 your user_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 calls provider.shutdown() or provider.force_flush() before it exits. BatchSpanProcessor exports in the background.
  • TelemetryMiddleware import fails: install the tracing extra, pip install "ag2[tracing]".
  • Events arrive but the conversation has no messages: the turn ran outside the agent_turn span. Call agent.ask() inside it.
  • Every turn is a separate conversation, or the user is missing: pass span_attributes with gen_ai.conversation.id and user.id on each turn.
  • Each model call appears twice: set open_telemetry=False on AnthropicConfig, or set ANTHROPIC_OPEN_TELEMETRY=false.
  • Model and tool events have no text: capture_content was set to False on TelemetryMiddleware.
  • You use AG2 0.x (from autogen import ConversableAgent): TelemetryMiddleware is part of the AG2 1.x API. Upgrade to ag2 1.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.