IntegrationsFrameworksLangChain DeepAgents
This is a Jupyter notebook

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

%pip install langfuse deepagents tavily-python

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).

import os
 
# Get keys for your project from the project settings page: https://cloud.langfuse.com
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..." 
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..." 
os.environ["LANGFUSE_HOST"] = "https://cloud.langfuse.com" # πŸ‡ͺπŸ‡Ί EU region
# os.environ["LANGFUSE_HOST"] = "https://us.cloud.langfuse.com" # πŸ‡ΊπŸ‡Έ US region
 
# Your API keys
os.environ["ANTHROPIC_API_KEY"] = "sk-..."
os.environ["TAVILY_API_KEY"] = "tvly-..."

Step 3: Initialize Langfuse

Configure the Langfuse client and initialize the LangChain callback handler for tracing.

from langfuse import get_client
from langfuse.langchain import CallbackHandler
 
# Initialize Langfuse client
langfuse = get_client()
 
# Verify connection
if langfuse.auth_check():
    print("Langfuse client is authenticated and ready!")
else:
    print("Authentication failed. Please check your credentials and host.")
 
# Initialize Langfuse CallbackHandler for LangChain (tracing)
langfuse_handler = CallbackHandler()

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]})

from typing import Literal
from tavily import TavilyClient
from deepagents import create_deep_agent
 
# Initialize Tavily client for web search
tavily_client = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])
 
# Web search tool
def internet_search(
    query: str,
    max_results: int = 5,
    topic: Literal["general", "news", "finance"] = "general",
    include_raw_content: bool = False,
):
    """Run a web search"""
    return tavily_client.search(
        query,
        max_results=max_results,
        include_raw_content=include_raw_content,
        topic=topic,
    )
 
 
# System prompt to steer the agent to be an expert researcher
research_instructions = """You are an expert researcher. Your job is to conduct thorough research, and then write a polished report.
 
You have access to an internet search tool as your primary means of gathering information.
 
## `internet_search`
 
Use this to run an internet search for a given query. You can specify the max number of results to return, the topic, and whether raw content should be included.
"""
 
# Create the deep agent
agent = create_deep_agent(
    tools=[internet_search],
    system_prompt=research_instructions,
)
 
# Invoke the agent with Langfuse tracing
result = agent.invoke(
    {"messages": [{"role": "user", "content": "What is Langfuse and how does it help with LLM observability?"}]}, 
    config={"callbacks": [langfuse_handler]}
)

LangChain DeepAgents Trace Example 1

Public example trace in Langfuse

Example 2: Advanced Agent with Sub-Agents

Now let’s create a more sophisticated agent that uses sub-agents for research and critique.

from deepagents import create_deep_agent
 
# Search tool for research
def internet_search(
    query: str,
    max_results: int = 5,
    topic: Literal["general", "news", "finance"] = "general",
    include_raw_content: bool = False,
):
    """Run a web search"""
    search_docs = tavily_client.search(
        query,
        max_results=max_results,
        include_raw_content=include_raw_content,
        topic=topic,
    )
    return search_docs
 
 
# Sub-agent for focused research
sub_research_prompt = """You are a dedicated researcher. Your job is to conduct research based on the users questions.
 
Conduct thorough research and then reply to the user with a detailed answer to their question
 
only your FINAL answer will be passed on to the user. They will have NO knowledge of anything except your final message, so your final report should be your final message!"""
 
research_sub_agent = {
    "name": "research-agent",
    "description": "Used to research more in depth questions. Only give this researcher one topic at a time. Do not pass multiple sub questions to this researcher. Instead, you should break down a large topic into the necessary components, and then call multiple research agents in parallel, one for each sub question.",
    "system_prompt": sub_research_prompt,
    "tools": [internet_search],
}
 
# Sub-agent for critique
sub_critique_prompt = """You are a dedicated editor. You are being tasked to critique a report.
 
You can find the report at `final_report.md`.
 
You can find the question/topic for this report at `question.txt`.
 
The user may ask for specific areas to critique the report in. Respond to the user with a detailed critique of the report. Things that could be improved.
 
You can use the search tool to search for information, if that will help you critique the report
 
Do not write to the `final_report.md` yourself.
 
Things to check:
- Check that each section is appropriately named
- Check that the report is written as you would find in an essay or a textbook - it should be text heavy, do not let it just be a list of bullet points!
- Check that the report is comprehensive. If any paragraphs or sections are short, or missing important details, point it out.
- Check that the article covers key areas of the industry, ensures overall understanding, and does not omit important parts.
- Check that the article deeply analyzes causes, impacts, and trends, providing valuable insights
- Check that the article closely follows the research topic and directly answers questions
- Check that the article has a clear structure, fluent language, and is easy to understand.
"""
 
critique_sub_agent = {
    "name": "critique-agent",
    "description": "Used to critique the final report. Give this agent some information about how you want it to critique the report.",
    "system_prompt": sub_critique_prompt,
}
 
 
# Main agent prompt
research_instructions = """You are an expert researcher. Your job is to conduct thorough research, and then write a polished report.
 
The first thing you should do is to write the original user question to `question.txt` so you have a record of it.
 
Use the research-agent to conduct deep research. It will respond to your questions/topics with a detailed answer.
 
When you think you enough information to write a final report, write it to `final_report.md`
 
You can call the critique-agent to get a critique of the final report. After that (if needed) you can do more research and edit the `final_report.md`
You can do this however many times you want until are you satisfied with the result.
 
Only edit the file once at a time (if you call this tool in parallel, there may be conflicts).
 
Here are instructions for writing the final report:
 
<report_instructions>
 
CRITICAL: Make sure the answer is written in the same language as the human messages! If you make a todo plan - you should note in the plan what language the report should be in so you dont forget!
Note: the language the report should be in is the language the QUESTION is in, not the language/country that the question is ABOUT.
 
Please create a detailed answer to the overall research brief that:
1. Is well-organized with proper headings (# for title, ## for sections, ### for subsections)
2. Includes specific facts and insights from the research
3. References relevant sources using [Title](URL) format
4. Provides a balanced, thorough analysis. Be as comprehensive as possible, and include all information that is relevant to the overall research question. People are using you for deep research and will expect detailed, comprehensive answers.
5. Includes a "Sources" section at the end with all referenced links
 
You can structure your report in a number of different ways. Here are some examples:
 
To answer a question that asks you to compare two things, you might structure your report like this:
1/ intro
2/ overview of topic A
3/ overview of topic B
4/ comparison between A and B
5/ conclusion
 
To answer a question that asks you to return a list of things, you might only need a single section which is the entire list.
1/ list of things or table of things
Or, you could choose to make each item in the list a separate section in the report. When asked for lists, you don't need an introduction or conclusion.
1/ item 1
2/ item 2
3/ item 3
 
To answer a question that asks you to summarize a topic, give a report, or give an overview, you might structure your report like this:
1/ overview of topic
2/ concept 1
3/ concept 2
4/ concept 3
5/ conclusion
 
If you think you can answer the question with a single section, you can do that too!
1/ answer
 
REMEMBER: Section is a VERY fluid and loose concept. You can structure your report however you think is best, including in ways that are not listed above!
Make sure that your sections are cohesive, and make sense for the reader.
 
For each section of the report, do the following:
- Use simple, clear language
- Use ## for section title (Markdown format) for each section of the report
- Do NOT ever refer to yourself as the writer of the report. This should be a professional report without any self-referential language.
- Do not say what you are doing in the report. Just write the report without any commentary from yourself.
- Each section should be as long as necessary to deeply answer the question with the information you have gathered. It is expected that sections will be fairly long and verbose. You are writing a deep research report, and users will expect a thorough answer.
- Use bullet points to list out information when appropriate, but by default, write in paragraph form.
 
REMEMBER:
The brief and research may be in English, but you need to translate this information to the right language when writing the final answer.
Make sure the final answer report is in the SAME language as the human messages in the message history.
 
Format the report in clear markdown with proper structure and include source references where appropriate.
 
<Citation Rules>
- Assign each unique URL a single citation number in your text
- End with ### Sources that lists each source with corresponding numbers
- IMPORTANT: Number sources sequentially without gaps (1,2,3,4...) in the final list regardless of which sources you choose
- Each source should be a separate line item in a list, so that in markdown it is rendered as a list.
- Example format:
  [1] Source Title: URL
  [2] Source Title: URL
- Citations are extremely important. Make sure to include these, and pay a lot of attention to getting these right. Users will often use these citations to look into more information.
</Citation Rules>
</report_instructions>
 
You have access to a few tools.
 
## `internet_search`
 
Use this to run an internet search for a given query. You can specify the number of results, the topic, and whether raw content should be included.
"""
 
# Create the agent with sub-agents
agent = create_deep_agent(
    tools=[internet_search],
    system_prompt=research_instructions,
    subagents=[critique_sub_agent, research_sub_agent],
)
 
# Invoke the agent with Langfuse tracing
result = agent.invoke(
    {"messages": [{"role": "user", "content": "What is Langfuse?"}]}, 
    config={"callbacks": [langfuse_handler]}
)

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

LangChain DeepAgents Trace Example

Public example trace in Langfuse

Interoperability with the Python SDK

You can use this integration together with the Langfuse Python SDK to add additional attributes to the trace.

The @observe() decorator provides a convenient way to automatically wrap your instrumented code and add additional attributes to the trace.

from langfuse import observe, get_client
 
langfuse = get_client()
 
@observe()
def my_instrumented_function(input):
 
    # Run your application here
    output = my_llm_call(input)
 
    langfuse.update_current_trace(
        input=input,
        output=output,
        user_id="user_123",
        session_id="session_abc",
        tags=["agent", "my-trace"],
        metadata={"email": "user@langfuse.com"},
        version="1.0.0"
    )
 
    return output

Learn more about using the Decorator in the Python SDK docs.

Next Steps

Once you have instrumented your code, you can manage, evaluate and debug your application:

Was this page helpful?