Skip to content

feat(claude-agent-sdk): accept a TraceConfig and use the shared tracer - #840

Merged
khushalsonawat merged 9 commits into
devfrom
feat/claude-agent-sdk-shared-tracer
Dec 3, 2026
Merged

khushalsonawat merged 9 commits into
devfrom
feat/claude-agent-sdk-shared-tracer

Conversation

@khushalsonawat

Copy link
Copy Markdown
Member

What does this PR do?

traceai-claude-agent-sdk now takes a TraceConfig and starts its spans on traceAI's shared tracer (FITracer), as the other framework packages do.

  • ClaudeAgentInstrumentor().instrument() and instrument_claude_agent_sdk() accept config, a TraceConfig. With no config, settings come from the FI_* environment variables when instrumentation starts. A config of another type raises TypeError.

  • A small key map (_privacy.py) tells TraceConfig which of this package's span keys carry input content, output content, or both:

    • input: claude_agent.prompt, claude_agent.system_prompt, claude_agent.tool.input and the fields taken from it (file path, command, pattern, URL, search query), and claude_agent.subagent.description / .prompt;
    • output: claude_agent.message.content, claude_agent.tool.output;
    • both: the conversation's output, and claude_agent.tool.error_message (a failing tool's output, also treated as error detail).

    The key names are unchanged.

  • SubagentTracker applies the same handling to the tracer it is given. Its constructor is unchanged.

    • A FITracer, or a wrapt proxy around one, keeps its own TraceConfig.
    • A FITracer whose settings cannot be read, a wrapper that is not a wrapt proxy but has a __wrapped__, or a chain of more than eight proxies: all content and error details hidden, and one warning naming the tracer's type.
    • Any other tracer gets a TraceConfig read from the environment. That includes a wrapper with no __wrapped__ around a FITracer.
  • Every span and event key the package writes is now a named constant in ClaudeAgentAttributes, with the same strings as before.

  • Requires fi-instrumentation-otel >= 1.3.0 (the Pydantic AI package's floor), and declares wrapt, which the package now imports.

Why?

So that a TraceConfig, passed as config or set through the FI_* environment variables, applies to the spans this package writes. traceAI's zero-code setup passes config= to every framework instrumentor.

What changes with no setting on

These are the shared tracer's defaults, the same as in the other framework packages. They are listed in the CHANGELOG.

  • An inline base64 image in an attribute value is replaced.
  • Session, user and metadata values set with traceAI's context helpers are attached.
  • FI_PII_REDACTION and traceAI's suppress-tracing switch are honoured.
  • An error status keeps the error message without a traceback or type prefix. A status that was only a traceback has no message. A failing tool's full response stays on claude_agent.tool.output, cut at 5,000 characters as before.
  • Spans that end in error carry error.type (_OTHER for a failing tool).

How was it tested?

  • Unit tests added / updated (pytest)
  • Integration tests pass: no real SDK run (see below)
  • ruff check / mypy: no new ruff check findings (ruff 0.17.3). Each touched file has the same count as dev or fewer. mypy not run; the package has no mypy config.
  • Public behaviour changes are documented in the package README (a TraceConfig example) and CHANGELOG ([Unreleased])

New tests, in tests/test_trace_config.py.

  • They drive ClaudeAgentInstrumentor through a stand-in Claude Agent SDK (a fake claude_agent_sdk module whose client calls the injected hooks) into a real TracerProvider with an in-memory exporter.
  • Each TraceConfig content setting, and the error-details setting, is checked on its own and in combination. Also checked: settings from the environment, instrument_claude_agent_sdk(config=…), PII redaction, and settings that should change nothing.
  • Further checks:
    • the error status texts and error.type with no setting on;
    • that every attribute constant is classified;
    • that every span key is a named constant (a scan of the package's source, itself tested on keys written as literals, names, f-strings, concatenations, dict(...) and **);
    • SubagentTracker with a plain tracer, a FITracer, wrapt proxies around each, a chain past the limit, and wrappers that are not proxies.
  • tests/test_subagent_tracker.py now starts spans on a real tracer provider, not a mock. The subagent nesting cases in tests/test_hooks.py also run through FITracer.

Results. The whole tests/ folder: 194 passed. Python 3.11.14, opentelemetry-sdk 1.45.3, wrapt 1.17.3, and this checkout's fi-instrumentation-otel source (1.5.2).

Not run:

  • fi-instrumentation-otel 1.3.0, the new floor. CI runs against the newest release from PyPI.
  • The real Claude Agent SDK, which needs the Claude CLI and an API key; the stand-in calls the same hooks the SDK calls.

Checklist

  • Branch is off dev
  • Commit messages follow Conventional Commits
  • No TODOs or commented-out code left in
  • No real API keys or secrets in the diff
  • If prose was added or changed: checked against docs/VOCABULARY.md. That file does not exist in the repo, so no check was possible.

Notes for reviewers

  • _privacy.py follows traceai_pydantic_ai/_privacy.py: an ObjectProxy over the caller's TraceConfig that overrides only mask, so error-detail, PII and image handling stay with the core.
  • SubagentTracker reads a given FITracer's settings in one helper (_settings_of), as cohere, crewai and together read _self_config. It uses object.__getattribute__ so a wrapped tracer's settings are not picked up by mistake. If the settings cannot be read, it logs a warning and replaces all content.

Drive ClaudeAgentInstrumentor through a stand-in SDK into real spans and
check each content key, the error status texts and error.type against the
TraceConfig settings, and that every span key is a classified named
constant. The subagent nesting cases also run through FITracer.

Signed-off-by: khushalsonawat <khushalsonawat@gmail.com>
ClaudeAgentInstrumentor().instrument() and instrument_claude_agent_sdk()
take config, a TraceConfig, as the other framework packages do; without
one, settings come from the environment when instrumentation starts.
Spans go through FITracer, and a small key map tells TraceConfig which of
this package's keys hold input or output content. Every span and event
key is now a named constant. Requires fi-instrumentation-otel 1.3.0.

Signed-off-by: khushalsonawat <khushalsonawat@gmail.com>
…ectly

Signed-off-by: khushalsonawat <khushalsonawat@gmail.com>
…r's TraceConfig

The tracker tests now start spans on a real tracer provider with an
in-memory exporter. New cases cover a plain tracer with settings from the
environment, a FITracer with its own TraceConfig, error ends, and that
every exported key is a classified constant.

Signed-off-by: khushalsonawat <khushalsonawat@gmail.com>
SubagentTracker starts its spans on a FITracer with this package's key
map. A FITracer it is given keeps its own TraceConfig, over the tracer
underneath; a plain tracer gets a TraceConfig read from the environment.
If a FITracer's TraceConfig cannot be read, the tracker logs a warning and
uses one with every content and error-details setting on.

Signed-off-by: khushalsonawat <khushalsonawat@gmail.com>
The scan now reports any key expression that is not a ClaudeAgentAttributes
attribute: names, f-strings, concatenations, dict() keywords and ** unpacking,
as well as string literals. Stores into the package's own lookup tables are
listed as known non-span keys.

Signed-off-by: khushalsonawat <khushalsonawat@gmail.com>
…xies and wrappers

Rows for a FITracer inside one or two wrapt proxies, a proxy around a
FITracer whose settings cannot be read, a chain of proxies past the limit,
and wrappers that are not proxies, around a FITracer and around a plain
tracer.

Signed-off-by: khushalsonawat <khushalsonawat@gmail.com>
…entTracker

SubagentTracker looks through wrapt proxies for a FITracer and uses its
TraceConfig. A wrapper it cannot look through, or a chain past the limit,
hides all content and error details and logs a warning. Any other tracer
keeps settings read from the environment.

Signed-off-by: khushalsonawat <khushalsonawat@gmail.com>
…er checks

The wrapper with no __wrapped__ now holds a FITracer whose own settings
differ from the environment's, so the row shows which ones are used. The
no-warning checks count only this package's logger, like the one-warning
check.

Signed-off-by: khushalsonawat <khushalsonawat@gmail.com>
@khushalsonawat
khushalsonawat marked this pull request as ready for review December 3, 2026 06:27
@khushalsonawat
khushalsonawat merged commit 2e94a54 into dev Dec 3, 2026
24 checks passed
@khushalsonawat
khushalsonawat deleted the feat/claude-agent-sdk-shared-tracer branch December 3, 2026 06:35
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.

2 participants