AgentOps works seamlessly with applications built using Langchain.

Adding AgentOps to Langchain applications

1

Install the AgentOps SDK and the additional Langchain dependency

pip install agentops
pip install agentops[langchain]

Give us a star on GitHub while you’re at it (you may be our 2,000th 😊)

2

Set up your import statements

Import the following Langchain and AgentOps dependencies

import os
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
from agentops.langchain_callback_handler import LangchainCallbackHandler

For more features see our Usage section.

3

Set up your Langchain handler to make the calls

Set up your Langchain agent with the AgentOps callback handler and AgentOps will automatically record your Langchain sessions.

handler = LangchainCallbackHandler(api_key=AGENTOPS_API_KEY, tags=['Langchain Example'])

llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY,
	callbacks=[handler],
	model='gpt-3.5-turbo')

agent = initialize_agent(tools,
	llm,
	agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
	verbose=True,
	callbacks=[handler], # You must pass in a callback handler to record your agent
	handle_parsing_errors=True)

Note that you don’t need to set up a separate agentops.init() call, as the Langchain callback handler will automatically initialize the AgentOps client for you.

4

Set your API key

Retrieve an API Key from your Settings > Projects & API Keys page.

Settings > Projects & API Keys

API keys are tied to individual projects.
A Default Project has been created for you, so just click Copy API Key

Set this API Key in your environment variables

.env
AGENTOPS_API_KEY=<YOUR API KEY>
5

Run your agent

Execute your program and visit app.agentops.ai/drilldown to observe your Langchain Agent! 🕵️

After your run, AgentOps prints a clickable url to console linking directly to your session in the Dashboard

Clickable link to session

Full Examples

import os
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
from agentops.langchain_callback_handler import LangchainCallbackHandler

handler = LangchainCallbackHandler(api_key=AGENTOPS_API_KEY, tags=['Langchain Example'])

llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY,
	callbacks=[handler],
	model='gpt-3.5-turbo')

agent = initialize_agent(tools,
	llm,
	agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
	verbose=True,
	callbacks=[handler], # You must pass in a callback handler to record your agent
	handle_parsing_errors=True)