Overview
The@contextcompany/claude package wraps your Claude Agent SDK instance to automatically capture:
- Agent queries and responses
- Streaming messages and events
- Tool calls and results
- Run metadata and session tracking
- Errors and exceptions
Installation
Basic Usage
Once wrapped, use your agent exactly as you would normally. The instrumentation is transparent:- The user prompt
- All streaming messages
- The complete response
- Timing information
Tracking Runs and Sessions
You can track runs and sessions by passing metadata through thetcc parameter:
If you don’t provide a
runId, Observatory will automatically generate one using UUID v4.Run IDs
Run IDs uniquely identify a single agent execution:Session IDs
Session IDs group multiple runs together (e.g., a conversation):Working with Tools
The instrumentation automatically captures tool definitions and executions:- Tool definitions passed to the agent
- Tool call arguments
- Tool execution results
- Tool execution timing
Custom Metadata
Add custom metadata to track additional context:Debug Mode
Enable debug mode to see detailed logging:- Query initialization
- Message collection
- Telemetry transmission
- API responses
Error Handling
Errors are automatically captured and reported:Submitting Feedback
Collect user feedback for specific runs:Environment Variables
Your Observatory API key. Required for sending telemetry data.
Custom ingestion endpoint. Only needed for self-hosted instances.
API Reference
instrumentClaudeAgent<T>(sdk: T): WrappedSDK<T>
Wraps a Claude Agent SDK instance with Observatory instrumentation.
Parameters:
sdk: Your Claude Agent SDK instance
- A wrapped SDK with the same interface, plus support for the
tccparameter inquery()calls
TCC Configuration
Thetcc parameter accepts the following options:
Unique identifier for this agent run. Auto-generated if not provided.
Session identifier for grouping related runs (e.g., a conversation).
Custom key-value metadata to attach to the run. All values must be JSON-serializable.
Enable debug logging to console.
How It Works
The instrumentation uses JavaScript Proxies to transparently wrap:- Query function: Intercepts the
agent.query()calls - Message stream: Collects all messages as they’re yielded
- Tool handlers: Wraps tool handlers to capture inputs and outputs
Performance Considerations
- Zero latency impact: Messages are collected in-memory and sent asynchronously after the stream completes
- Memory overhead: Minimal - only stores messages until the stream ends
- Network: Single HTTP request per agent query after completion
Troubleshooting
No data appearing in Observatory
No data appearing in Observatory
- Verify
TCC_API_KEYis set correctly - Enable debug mode to see transmission logs:
- Check for errors in the console
- Ensure the stream completes (data is sent after completion)
TypeScript errors
TypeScript errors
If you get type errors about the
tcc parameter, ensure:- Your TypeScript version is 5.0 or higher
- You’re importing the correct types:
Tool calls not showing
Tool calls not showing
Tool instrumentation happens automatically, but:
- Ensure tools are defined using
agent.tool() - Enable debug mode to verify tool calls are being captured
- Check that tools are actually being invoked by the agent
Metadata not appearing
Metadata not appearing
Metadata is stored exactly as provided. Ensure:
- Values are JSON-serializable (no functions, circular references, etc.)
- Keys don’t start with
tcc.(reserved prefix) - Debug mode is enabled to see what’s being sent
Next Steps
Configuration
Learn about configuration options
Session Tracking
Learn more about tracking conversational sessions
Feedback Collection
Set up user feedback collection
API Reference
Complete API documentation
