Bitfab integrates with the Claude Agent SDK via a handler that automatically captures LLM turns, tool invocations, and subagent execution as traced spans. The handler instruments the SDK’s hook system and wraps the response stream to capture all execution data. Canonical signatures: TypeScript reference · Python referenceDocumentation Index
Fetch the complete documentation index at: https://docs.bitfab.ai/llms.txt
Use this file to discover all available pages before exploring further.
Supported Languages
| Language | Method | Status |
|---|---|---|
| TypeScript | getClaudeAgentHandler() | ✅ Supported |
| Python | get_claude_agent_handler() | ✅ Supported |
| Ruby | — | Not yet supported |
| Go | — | Not yet supported |
Quick Start
What Gets Captured
The handler captures three types of spans:| Event | Span Type | Captured Data |
|---|---|---|
| LLM turns | llm | Full conversation history, assistant response content, model name, token usage (input, output, cache read, cache creation) |
| Tool invocations | function | Tool input, tool response or error |
| Subagent execution | agent | Agent type, start/stop lifecycle |
Token Usage
For LLM spans, token usage is extracted from the message stream:inputTokens— Input tokensoutputTokens— Output tokenscacheReadTokens— Cache read tokens (if applicable)cacheCreationTokens— Cache creation tokens (if applicable)model— Model name
TypeScript
Installation
Method Signature
traceFunctionKey(string, required) — Groups all traces from this handler under one key in Bitfab
BitfabClaudeAgentHandler instance with methods for instrumenting options and wrapping streams.
Handler Methods
instrumentOptions(options)
Injects Bitfab hooks into the SDK options object. Mutates the options in-place and returns them.
- PreToolUse → Creates a
functionspan with the tool input - PostToolUse → Completes the span with the tool response
- PostToolUseFailure → Completes the span with an error
- SubagentStart → Creates an
agentspan - SubagentStop → Completes the agent span
wrapResponse(stream)
Wraps the receiveResponse() async iterable to capture LLM turns from the message stream. Messages are yielded unchanged.
llm span containing:
- Input: Full conversation history snapshot up to this turn
- Output: Assistant message content blocks
- Context: Model name, token usage
wrapQuery(stream)
Simpler alternative for the query() API (no tool support):
Usage
Nesting with Core Tracing
Error Handling
- All hook callbacks are wrapped in try/catch — errors are silently ignored and return an empty object.
- Stream processing continues even if individual message capture fails.
- The handler never throws or affects the SDK’s execution.
Python
Installation
Method Signature
trace_function_key(str, required) — Groups all traces from this handler under one key in Bitfab
BitfabClaudeAgentHandler instance with methods for instrumenting options and wrapping streams.
Handler Methods
instrument_options(options)
Injects Bitfab hooks into the SDK options object. Returns the modified options.
wrap_response(stream)
Wraps the receive_response() async iterator to capture LLM turns:
wrap_query(stream)
Simpler alternative for the query() API:
Usage
Nesting with Core Tracing
Error Handling
- All hook callbacks are wrapped in try/except — errors are logged at DEBUG level and return an empty dict.
- Stream processing continues even if individual message capture fails.
- The handler never raises or affects the SDK’s execution.