Skip to content

Add first-class agent identity tracking (OTel GenAI semantic conventions)#3

Merged
prathamesh-sonpatki merged 6 commits intomainfrom
feat/agent-identity-tracking
Apr 20, 2026
Merged

Add first-class agent identity tracking (OTel GenAI semantic conventions)#3
prathamesh-sonpatki merged 6 commits intomainfrom
feat/agent-identity-tracking

Conversation

@prathamesh-sonpatki
Copy link
Copy Markdown
Member

@prathamesh-sonpatki prathamesh-sonpatki commented Mar 16, 2026

Summary

  • Add 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)
  • Auto-propagate agent attributes to all child spans via Last9SpanProcessor
  • Add create_agent / invoke_agent operation names per the OTel spec

Why

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

from last9_genai import agent_context, conversation_context

with conversation_context(conversation_id="session_123"):
    with agent_context(agent_id="router_v1", agent_name="Router Agent"):
        # spans get gen_ai.agent.id="router_v1", gen_ai.agent.name="Router Agent"
        route = classify_intent(query)

    with agent_context(agent_id="support_v2", agent_name="Support Agent", agent_version="2.0"):
        response = handle_support(query)

Changes

File What
last9_genai/core.py Add GenAIAttributes.AGENT_* constants, Operations.CREATE_AGENT/INVOKE_AGENT
last9_genai/context.py Add agent_context() context manager + contextvars
last9_genai/processor.py Auto-propagate agent attrs in on_start()
last9_genai/__init__.py Export agent_context
README.md Document agent tracking in features, quick start, and attributes reference
examples/agent_tracking.py New example: single agent, multi-agent routing, agent+workflow nesting
examples/context_tracking.py Add agent context example

Test plan

  • All 117 existing tests pass
  • Verify agent_context propagates gen_ai.agent.id to child spans
  • Verify nesting with conversation_context and workflow_context (triple-nest, handoff, inner override, mixed exit — all pass)
  • Run examples/agent_tracking.py end-to-end — 3 examples pass, spans carry gen_ai.agent.*, gen_ai.conversation.id, workflow.* as expected

🤖 Generated with Claude Code

prathamesh-sonpatki and others added 3 commits March 16, 2026 11:47
…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>
Comment thread tests/test_context.py Fixed
Comment thread tests/test_context.py Fixed
@prathamesh-sonpatki
Copy link
Copy Markdown
Member Author

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

🤖 Generated with Claude Code

prathamesh-sonpatki and others added 2 commits April 20, 2026 01:21
…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>
Comment thread tests/test_context.py Fixed
Comment thread tests/test_context.py Fixed
Comment thread tests/test_context.py Fixed
Comment thread tests/test_context.py Fixed
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 prathamesh-sonpatki merged commit da6cfc2 into main Apr 20, 2026
7 checks passed
@prathamesh-sonpatki prathamesh-sonpatki deleted the feat/agent-identity-tracking branch April 20, 2026 04:00
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>
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant