Weave for Agents is in public preview. Features, APIs, and the Agents view UI may change before general availability.
What you’ll learn
By the end of this quickstart, you’ll have a working multi-turn agent that emits Weave-compatible OTel spans. You’ll also understand how Weave maps sessions, turns, LLM calls, and tool calls onto your agent code so you can apply the same pattern to your own custom agents. The code in this guide sets up a small research agent that can look things up on Wikipedia. It asks three questions (three turns) and uses the LLM to choose when to search Wikipedia for an answer. Weave records every step (the conversation, each question, each AI response, and each Wikipedia lookup) so you can see 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
start_session/startSessionandstart_turn/startTurn. - Wrap LLM calls with
start_llm/startLLMand record usage. - Wrap tool executions with
start_tool/startTooland 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.| Concept | Python | TypeScript | OTel span |
|---|---|---|---|
| A conversation | weave.start_session(...) | weave.startSession(...) | (no span, groups turns) |
| One user or agent exchange | weave.start_turn(...) | weave.startTurn(...) | invoke_agent |
| One LLM API call | weave.start_llm(...) | weave.startLLM(...) | chat |
| One tool execution | weave.start_tool(...) | weave.startTool(...) | execute_tool |
with weave.start_*(...) as obj:). On exit, they end the span and flush attributes, including on exceptions. In TypeScript, call .end() on each returned object. Use try { ... } finally { obj.end(); } to guarantee cleanup on exceptions.
Other GenAI semantic-convention attributes, such as gen_ai.usage.* and gen_ai.agent.name, enable additional rendering, but they’re optional.
Prerequisites
- A W&B account and API key.
- An OpenAI API key.
- Python 3.10+ (for the Python examples).
- Node.js 18+ (the TypeScript examples require built-in
fetch).
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 doesn’t 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 along with an OpenAI tool schema that specifies when and how to use the tool.Run a traced multi-turn agent
With the tool and Weave initialization in place, the next step combines them into a complete agent loop. This loop shows how sessions, turns, LLM calls, and tool calls nest together. The following example runs three turns in a single session. Each turn:- Opens a
chatspan and lets the LLM choose whether to call the tool. - If the LLM requests 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
- Learn how to trace agents with Weave and what features and options are available in the Weave SDK.
- See Trace agent integrations for more options about how to integrate Weave with your agents.