Skip to content

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

Open
prathamesh-sonpatki wants to merge 3 commits intomainfrom
feat/agent-identity-tracking
Open

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

Conversation

@prathamesh-sonpatki
Copy link
Member

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
  • Run examples/agent_tracking.py end-to-end

🤖 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>

def test_agent_context_cleanup(self, tracer_setup):
"""Test that agent context is cleaned up after exit"""
tracer, memory_exporter = tracer_setup

def test_agent_context_cleanup(self, tracer_setup):
"""Test that agent context is cleaned up after exit"""
tracer, memory_exporter = tracer_setup
@prathamesh-sonpatki
Copy link
Member Author

Code review

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

🤖 Generated with Claude Code

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