- Setting the
LANGCHAIN_WANDB_TRACINGenvironment variable to “true”. - Using a context manager with
wandb_tracing_enabled()to trace a particular block of code.
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.
Join us May 13th & May 14th at Interrupt, the Agent Conference by LangChain. Buy tickets >
Integrate with Weights & Biases tracing using LangChain Python.
LANGCHAIN_WANDB_TRACING environment variable to “true”.wandb_tracing_enabled() to trace a particular block of code.import os
from langchain_community.callbacks import wandb_tracing_enabled
os.environ["LANGCHAIN_WANDB_TRACING"] = "true"
# wandb documentation to configure wandb using env variables
# https://docs.wandb.ai/guides/track/advanced/environment-variables
os.environ["WANDB_PROJECT"] = "langchain-tracing"
from langchain.agents import create_agent, load_tools
from langchain_openai import OpenAI
# Agent run with tracing. Ensure OPENAI_API_KEY is set appropriately to run this example.
llm = OpenAI(temperature=0)
tools = load_tools(["llm-math"], llm=llm)
agent = create_agent(
model=llm,
tools=tools,
verbose=True,
)
agent.invoke("What is 2 raised to .123243 power?")
# Now, we unset the environment variable and use a context manager.
if "LANGCHAIN_WANDB_TRACING" in os.environ:
del os.environ["LANGCHAIN_WANDB_TRACING"]
# enable tracing using a context manager
with wandb_tracing_enabled():
agent.invoke("What is 5 raised to .123243 power?")
agent.invoke("What is 2 raised to .123243 power?")
> Entering new AgentExecutor chain...
I need to use a calculator to solve this.
Action: Calculator
Action Input: 5^.123243
Observation: Answer: 1.2193914912400514
Thought: I now know the final answer.
Final Answer: 1.2193914912400514
> Finished chain.
> Entering new AgentExecutor chain...
I need to use a calculator to solve this.
Action: Calculator
Action Input: 2^.123243
Observation: Answer: 1.0891804557407723
Thought: I now know the final answer.
Final Answer: 1.0891804557407723
> Finished chain.
'1.0891804557407723'
Was this page helpful?