LangChain
The@mutagent/langchain package provides a MutagentCallbackHandler that plugs into LangChain’s callback system. Every LLM call, chain execution, tool invocation, and retriever query is automatically captured as a trace span with zero code changes to your existing LangChain logic.
Installation
@mutagent/sdk >=0.1.0, @langchain/core >=1.1.8
Quick Start
Initialize tracing
Call
initTracing() once at application startup. This configures the SDK’s span batching and transport layer.Create the callback handler
Instantiate
MutagentCallbackHandler. Optionally pass session and user context.Full Example
What Gets Traced
The callback handler captures the following LangChain event types:| Event | Span Kind | Data Captured |
|---|---|---|
handleLLMStart / handleChatModelStart | llm.chat | Input prompts or messages, model name |
handleLLMEnd | llm.chat | Output text, token usage (prompt, completion, total) |
handleChainStart | chain | Chain name, input values |
handleChainEnd | chain | Output values |
handleToolStart | tool | Tool name, input string |
handleToolEnd | tool | Output string |
handleRetrieverStart | retrieval | Retriever name, query string |
handleRetrieverEnd | retrieval | Retrieved documents (content + metadata) |
| Error handlers | Any | Error message and status |
Token Usage Tracking
Token metrics are automatically extracted fromLLMResult.llmOutput.tokenUsage when available:
inputTokens— prompt tokensoutputTokens— completion tokenstotalTokens— combined total
Token usage availability depends on your LLM provider. OpenAI and Anthropic models report tokens; some open-source models may not.
Handler Options
MutagentCallbackHandler accepts optional configuration for session tracking, user attribution, and metadata:
| Option | Type | Description |
|---|---|---|
sessionId | string | Group related traces into a session |
userId | string | Attribute traces to a specific user |
tags | string[] | Tags for filtering in the dashboard |
metadata | Record<string, unknown> | Custom key-value metadata |
initTracing():
CLI Shortcut
Generate a complete integration scaffold with the CLI:package.json (looks for langchain or @langchain/core) and generates ready-to-use configuration code.
To validate an existing integration setup: