Notebooks
L
Langfuse
Integration Claude Agent Sdk

Integration Claude Agent Sdk

observabilityllmsgenaicookbookprompt-managementhacktoberfestlarge-language-modelsnextraLangfuselangfuse-docs

Integrate Langfuse with Claude Agent SDK

Python JS/TS

This notebook demonstrates how to capture detailed traces from the Claude Agent SDK with Langfuse using OpenTelemetry.

What is Claude Agent SDK?
The Claude Agent SDK is Anthropic's open-source framework for building AI agents. It provides a clean API for creating tool-using agents, including native support for MCP.

What is Langfuse?
Langfuse is the open source LLM engineering platform. It provides detailed tracing, monitoring, and analytics for every prompt, model response, and tool call in your agent, making it easy to debug, evaluate, and iterate on LLM applications.

Step 1: Install Dependencies

[ ]

Step 2: Set Up Environment Variables

Set up your Langfuse API keys (Langfuse Cloud or self-hosted) and your Anthropic API key (Anthropic Console).

[ ]

With the environment variables set, we can now initialize the Langfuse client. get_client() initializes the Langfuse client using the credentials provided in the environment variables.

[ ]

Step 3: OpenTelemetry Instrumentation

Use LangChain's Claude Agent SDK instrumentation library to instrument the agent SDK and send OpenTelemetry spans to Langfuse.

[ ]

Step 4: Build a Hello World Agent

Every tool call and model completion is captured as an OpenTelemetry span and forwarded to Langfuse.

[ ]

Step 5: View the Trace in Langfuse

Head over to your Langfuse dashboard → Traces. You should see traces including all tool calls and model inputs/outputs.

Claude Agent SDK example trace in Langfuse

Link to trace in Langfuse