Skip to content

feat(closes OPEN-10635): Claude Agent SDK TypeScript integration#208

Merged
gustavocidornelas merged 1 commit into
mainfrom
vini/open-10633-integration-add-claude-agent-sdk-support
Jun 5, 2026
Merged

feat(closes OPEN-10635): Claude Agent SDK TypeScript integration#208
gustavocidornelas merged 1 commit into
mainfrom
vini/open-10633-integration-add-claude-agent-sdk-support

Conversation

@viniciusdsmello

@viniciusdsmello viniciusdsmello commented May 12, 2026

Copy link
Copy Markdown
Contributor

Summary

First-party Openlayer tracing for the Claude Agent SDK (@anthropic-ai/claude-agent-sdk on npm).

  • Drop-in: import { query } from "openlayer/lib/integrations/claudeAgentSdk" — same signature as the SDK's query, auto-traced.
  • traceClaudeAgentSdk() runtime-patch init for codebases that can't change imports; patches query and ClaudeSDKClient.prototype idempotently.
  • Hooks compose with user-provided hooks (PreToolUse / PostToolUse / PostToolUseFailure); we never replace.
  • Captures: root AGENT step per query(), nested CHAT_COMPLETION per assistant turn, TOOL per tool call. Subagents nest via parent_tool_use_id. MCP tools parsed (mcp__server__tool). Cost/tokens/duration/session_id/agent_config on root. Per-query state isolated via AsyncLocalStorage. Stream is a pure observer.
  • New subpath export ./integrations/claude-agent-sdk and @anthropic-ai/claude-agent-sdk declared as optional peerDependency (^0.2.111).

Companion Python PR at openlayer-ai/openlayer-python#641 (OPEN-10634).

Test plan

  • 15 unit tests pass: yarn jest tests/integrations/claudeAgentSdk.test.ts
  • Live integration test runs end-to-end against the real SDK (gated on ANTHROPIC_API_KEY)
  • Example script examples/tracing/claude-agent-sdk/claudeAgentSdkTracing.ts runs end-to-end via npx tsx
  • yarn build succeeds and produces correct ESM/CJS/types for the new subpath export
  • yarn lint clean

Plan deviations (documented in individual commits)

  1. @anthropic-ai/claude-agent-sdk is ESM-only; loader uses require() with a dynamic-import() fallback so Jest workers and older Node both work.
  2. Cost/tokens stored directly on the AgentStep instance because the base Step.log() filters unknown keys.
  3. Added tsx as a devDependency so the example script can be smoke-tested locally.

Closes OPEN-10635.

🤖 Generated with Claude Code

@viniciusdsmello viniciusdsmello force-pushed the vini/open-10633-integration-add-claude-agent-sdk-support branch from b6cbfdd to 21b1225 Compare May 22, 2026 18:44
@viniciusdsmello viniciusdsmello self-assigned this May 22, 2026
@viniciusdsmello viniciusdsmello force-pushed the vini/open-10633-integration-add-claude-agent-sdk-support branch from 21b1225 to 90e07e5 Compare May 22, 2026 18:50
Add tracing integration for Anthropic's Claude Agent SDK
(@anthropic-ai/claude-agent-sdk on npm).

- Drop-in import: `import { query } from "openlayer/lib/integrations/claudeAgentSdk"`
  exposes a query() with the same signature as the SDK's, auto-traced.
- One-line setup alternative: traceClaudeAgentSdk() monkey-patches the
  SDK's query and ClaudeSDKClient prototype for codebases that can't
  change imports. Idempotent.
- Trace shape: root AGENT step "Claude Agent SDK query" per query()
  call, with nested CHAT_COMPLETION per assistant turn and TOOL per
  tool call. Subagent dispatches via the Agent tool become nested
  AGENT steps; the subagent's chats and tools nest underneath via
  parent_tool_use_id.
- Hooks (PreToolUse / PostToolUse / PostToolUseFailure) compose with
  any user-provided hooks; we never replace.
- AsyncLocalStorage isolates per-query state so concurrent queries
  don't trample each other's bookkeeping.
- New subpath export: ./integrations/claude-agent-sdk.
  @anthropic-ai/claude-agent-sdk declared as an optional
  peerDependency (^0.2.111).
- Tracer change limited to exporting createStep / getCurrentStep as
  internal helpers so the integration can drive step lifecycle.

Captures: cost, tokens, duration, session_id, model, systemPrompt,
agent_config (resolved tools / mcp_servers / skills / plugins),
agents_defined (subagent definitions), permission_denials, modelUsage,
raw assistant messages, raw ResultMessage. MCP server env / headers /
authorization are redacted by default.

Tests: 17 unit tests + 1 live integration test (gated on
ANTHROPIC_API_KEY). Live test runs successfully against the real SDK
and Openlayer ingest.

Examples: examples/claude-agent-sdk-tracing.ts (basic + subagent
scenarios) and examples/claude-agent-sdk-multi-agent.ts (in-process
MCP server, multiple subagents, end-to-end audit report). Layout
matches repo conventions (flat, kebab-case).

Closes OPEN-10635. Parallel Python work in openlayer-ai/openlayer-python
under OPEN-10634.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@viniciusdsmello viniciusdsmello force-pushed the vini/open-10633-integration-add-claude-agent-sdk-support branch from 90e07e5 to 6dc052a Compare May 26, 2026 13:55
@gustavocidornelas gustavocidornelas merged commit 9365929 into main Jun 5, 2026
5 checks passed
@gustavocidornelas gustavocidornelas deleted the vini/open-10633-integration-add-claude-agent-sdk-support branch June 5, 2026 13:21
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