Try in Colab · GitHub source The Weave SDK allows you to trace custom agents or agents created using popular SDKs. This quickstart guides you through how to manually integrate Weave into a custom-built multi-turn agent to emit and capture OpenTelemetry spans and render them in Weave’s Agents view. If you are looking to integrate Weave with popular SDKs or harnesses, such as the Claude Agents SDK or Codex, see the Weave integration section. Weave autopatches into several popular agent-building SDKs and agent harnesses for quick integration.Documentation Index
Fetch the complete documentation index at: https://wb-21fd5541-weave-agents.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
What you’ll learn
The code in this guide sets up a small research agent that can look things up on Wikipedia. It asks three questions (three turns), lets the AI decide when to search Wikipedia for an answer, and uses Weave to record every step (the conversation, each question, each AI response, and each Wikipedia lookup) so you can see exactly what happened in the Weave Agents view. This guide shows you how to:- Initialize Weave for agent tracing with
weave.init() - Open a session and a turn with
weave.start_session()andweave.start_turn() - Wrap LLM calls with
weave.start_llm()and record usage - Wrap tool executions with
weave.start_tool()and record results - View the resulting session, turns, and tool calls in the Agents view
How the Weave SDK works with agents
The Weave SDK includes a generic OTel ingest system for agents, meaning that Weave can capture information from any OTel span in your agent’s code. However, Weave requires special handling of the following spans to render your agent’s traces in the Agents view of the Weave UI.| Function | Maps to | OTel span |
|---|---|---|
weave.start_session(...) | A conversation | (no span — groups turns) |
weave.start_turn(...) | One user / agent exchange | invoke_agent |
weave.start_llm(...) | One LLM API call | chat |
weave.start_tool(...) | One tool execution | execute_tool |
gen_ai.usage.* and gen_ai.agent.name, enable additional rendering, but they are optional.
Prerequisites
- A W&B account and API key
- Python 3.10+
- An OpenAI API key
Install packages
Install the following packages into your developer environment:Initialize Weave
weave.init() authenticates with W&B and configures the OTel exporter that sends agent spans to the Agents view. If the project does not exist on your team, Weave creates it the first time you write to it.
Define a tool
The following code defines the agent’s Wikipedia search tool and an OpenAI tool schema to determine when and how to use the tool.Run a traced multi-turn agent
The example below runs three turns in a single session. Each turn:- Opens a
chatspan and lets the LLM decide whether to call the tool - If the LLM requested a tool, opens an
execute_toolspan around the call and feeds the result back to the LLM - Opens a second
chatspan to produce the final answer
See your agent traces in the Agents view
Whenweave.init() runs, it prints a link to your project where you can see:
- A row in the Agents tab for
research-bot - One session containing three turns
- Each turn (
invoke_agent) with twochatspans and anexecute_toolspan nested inside - Token counts, latency, model, and the full message exchange on each
chat
Next steps
- Get a better understanding of how to trace agents with Weave and what features and options are available in the Weave SDK.
- See the integration section for more options on how to integrate Weave with your agents.