Observing agents built with LangChain
LangChain is a Python framework for building AI-driven applications that use large language models (LLMs) and external tools. With LangChain, you can develop intelligent workflows that chain together tasks, tools, and LLMs to create context-aware systems for documentation generation, summarization, data analysis, and more.
Instana provides observability for LangChain applications, enabling real-time monitoring of workflows, LLM interactions, and task execution.
Prerequisites
Make sure that the following prerequisites are met.
-
Python 3.8 or later is installed.
-
An LLM API key, Anthropic, OpenAI, or any LLM provider supported by LangChain.
-
Instana is configured for your application, see Getting started.
Instrumenting LangChain applications
To instrument your LangChain application with Instana, complete the following steps:
-
Install the required packages for LangChain and OpenLLMetry.
pip install langchain langchain-anthropic traceloop-sdk -
Set your LLM provider credentials.
export ANTHROPIC_API_KEY="<your-api-key>"Note:Replace
ANTHROPIC_API_KEYwith your LLM provider's environment variable if using a different provider (e.g.,OPENAI_API_KEYfor OpenAI). -
Create your LangChain application.
A LangChain workflow monitoring with OpenLLMetry is shown in the following example:
from langchain_anthropic import ChatAnthropic from langchain_core.prompts import PromptTemplate from traceloop.sdk import Traceloop from traceloop.sdk.decorators import task, workflow # Initialize OpenLLMetry Traceloop.init(app_name="langchain_demo") @task(name="initialize_llm") def initialize_model(): return ChatAnthropic( model="claude-3-5-sonnet-20241022", temperature=0.7, max_tokens=512 ) @task(name="create_prompt_template") def create_prompt(): return PromptTemplate( input_variables=["input_text"], template="You are a helpful AI assistant. Respond to the following: {input_text}" ) @task(name="process_llm_query") def process_query(prompt, llm, input_text): chain = prompt | llm return chain.invoke({"input_text": input_text}) @workflow(name="langchain_conversation_workflow") def run_conversation(): llm = initialize_model() prompt_template = create_prompt() input_text = "Explain the concept of quantum computing in simple terms." response = process_query(prompt_template, llm, input_text) print("Response:", response.content) if __name__ == "__main__": run_conversation() -
Run your LangChain application.
python langchain_app.pyThe application generates a response:
Response: Quantum computing is a type of computing that uses quantum-mechanical phenomena such as superposition and quantum entanglement to perform calculations. Unlike classical computers that use binary digits (0s and 1s), quantum computers use quantum bits or "qubits" that can exist in multiple states simultaneously. This allows quantum computers to perform certain types of calculations much faster than classical computers. -
View data on Instana. After running your application, data will appear in the Instana Gen AI observability dashboard:
Figure 1. LangChain traces in Instana
The dashboard shows:
-
Workflow execution flow
-
Individual task execution (model initialization, prompt creation, query processing)
-
LLM interactions with latency and token usage
-
Chain operations
-
Troubleshooting
For common issues such as traces not appearing or connection errors, see Troubleshooting.
Metrics not appearing with expected values when using create_react_agent
Issue: Some metrics, including the model name, are not listed correctly when using create_react_agent.
Possible cause: Importing from the langchain.agents package is not supported.
To troubleshoot this issue, import create_react_agent from the langgraph.prebuilt package:
# Don't use this
from langchain.agents import create_react_agent
# Use this instead
from langgraph.prebuilt import create_react_agent
Next steps
-
Explore other AI agent frameworks supported by Instana
-
Learn about cost calculation for your LLM usage
-
Set up alerts for your LangChain workflows