Integration Langchain Deepagents
Integrate Langfuse with LangChain DeepAgents
This notebook demonstrates how to integrate Langfuse with LangChain DeepAgents. By the end of this notebook, you will be able to trace your DeepAgents applications with Langfuse for improved observability and debugging.
What are LangChain DeepAgents? DeepAgents is a framework for building autonomous AI agents that can perform deep research and complex tasks. Built on LangChain, DeepAgents provides tools for creating agents that can plan, research, critique, and iterate on their outputs.
What is Langfuse? Langfuse is an open-source LLM engineering platform. It provides tracing and monitoring capabilities for LLM applications, helping developers debug, analyze, and optimize their AI systems. Langfuse integrates with various tools and frameworks via native integrations, OpenTelemetry, and API/SDKs.
Get Started
We'll walk through examples of using LangChain DeepAgents and integrating it with Langfuse.
Step 1: Install Dependencies
Step 2: Set Up Environment Variables
Get your Langfuse API keys by signing up for Langfuse Cloud or self-hosting Langfuse. You'll also need your Anthropic API key (for Claude) and Tavily API key (for web search).
Step 3: Initialize Langfuse
Configure the Langfuse client and initialize the LangChain callback handler for tracing.
Example 1: Create a Simple Research Agent
Let's start with a simple example that creates a research agent with web search capabilities.
To instrument your agent, add the callback to the agent invocation:
agent.invoke(config={"callbacks": [langfuse_handler]})
Example 2: Advanced Agent with Sub-Agents
Now let's create a more sophisticated agent that uses sub-agents for research and critique.
Step 6: See Traces in Langfuse
After running the agent examples above, you can view the traces generated by your DeepAgents in Langfuse. The traces will show the complete execution flow including:
- Agent planning and reasoning steps
- Tool invocations (web searches)
- Sub-agent interactions
- Token usage and latencies
- Full conversation history

