Add first-class agent identity tracking (OTel GenAI semantic conventions)#3
Merged
prathamesh-sonpatki merged 6 commits intomainfrom Apr 20, 2026
Merged
Conversation
…ventions Add gen_ai.agent.id, gen_ai.agent.name, gen_ai.agent.description, and gen_ai.agent.version attributes following the OpenTelemetry GenAI semantic conventions for agent spans. This enables tracking agent identity across multi-agent systems with automatic context propagation. - Add agent_context() context manager (same pattern as conversation_context) - Auto-propagate agent attributes via Last9SpanProcessor - Add create_agent/invoke_agent operation names - Add agent_tracking.py example - Update README and context_tracking example Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
12 new tests covering: - Basic agent_id propagation to spans - All fields (id, name, version) - Nested span propagation - Context cleanup after exit - Inner context overrides outer - Sequential multi-agent (routing pattern) - Nesting with conversation_context - Nesting with workflow_context - Triple nesting (conversation + agent + workflow) - Multi-agent handoff within same conversation - No-span edge case Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Member
Author
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. 🤖 Generated with Claude Code |
…nused Operations - agent_context() now accepts agent_description (maps to gen_ai.agent.description) and wires it through the span processor, matching OpenAI Agents SDK and autogen-core emission. - agent_name is now required (first positional) and agent_id is optional. Per OTel GenAI semconv, gen_ai.agent.name is required and gen_ai.agent.id is recommended. - Drop unused Operations.CREATE_AGENT / INVOKE_AGENT. These were added but had no callers and will be re-added as part of the full OTel-semconv cleanup in ENG-896 (which also fixes CHAT_COMPLETIONS / TEXT_COMPLETION / TOOL_CALL). - Extract the processor's reserved-key list into a module-level frozenset so adding a new typed context var updates both branches in one place. - Restore _custom_attributes on agent_context exit (was dropped in the previous revision if **custom_attrs were passed). - Document coexistence with native-instrumented agent frameworks (AutoGen, OpenAI Agents SDK): those frameworks set gen_ai.agent.name directly on their own invoke_agent / create_agent spans inside the span body, which overrides our on_start injection. agent_context still tags sibling / child spans correctly. - Tests updated for new signature + agent_description propagation + custom_attrs restoration. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
github-code-quality bot flagged three agent tests that unpacked (tracer, memory_exporter) but only asserted on contextvars state. Replace with either `_` for the unused element or drop the unpack entirely (keeping the fixture parameter so Last9SpanProcessor setup still runs). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
prathamesh-sonpatki
added a commit
that referenced
this pull request
Apr 20, 2026
Bumps version to 1.2.0 and updates CHANGELOG with the agent_context() addition from PR #3. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
4 tasks
prathamesh-sonpatki
added a commit
that referenced
this pull request
Apr 20, 2026
Bumps version to 1.2.0 and updates CHANGELOG with the agent_context() addition from PR #3. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
agent_context()context manager for tracking agent identity using OTel GenAI semantic conventions (gen_ai.agent.id,gen_ai.agent.name,gen_ai.agent.description,gen_ai.agent.version)Last9SpanProcessorcreate_agent/invoke_agentoperation names per the OTel specWhy
The OpenTelemetry GenAI SIG defines first-class agent attributes (experimental). Major frameworks (OpenAI Agents SDK, CrewAI, AutoGen) already emit these. This aligns the SDK with the emerging standard so spans are natively understood by any OTel-compatible backend.
Usage
Changes
last9_genai/core.pyGenAIAttributes.AGENT_*constants,Operations.CREATE_AGENT/INVOKE_AGENTlast9_genai/context.pyagent_context()context manager + contextvarslast9_genai/processor.pyon_start()last9_genai/__init__.pyagent_contextREADME.mdexamples/agent_tracking.pyexamples/context_tracking.pyTest plan
agent_contextpropagatesgen_ai.agent.idto child spansconversation_contextandworkflow_context(triple-nest, handoff, inner override, mixed exit — all pass)examples/agent_tracking.pyend-to-end — 3 examples pass, spans carrygen_ai.agent.*,gen_ai.conversation.id,workflow.*as expected🤖 Generated with Claude Code