What you’ll learn
By the end of this quickstart, you’ll have a working multi-turn agent that emits Agent Lens-compatible OTel spans. You’ll also understand how Agent Lens maps conversations, 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 Python or TypeScript 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. Agent Lens records every step (the conversation, each question, each AI response, and each Wikipedia lookup) so you can see what happened in the Agent Lens Conversations tab. This guide shows you how to:- Initialize Agent Lens for agent tracing with
tracing.init(). - Open a conversation and a turn with
start_conversation/startConversationandstart_turn/startTurn. - Wrap LLM calls with
start_llm/startLLMand record usage. - Wrap tool executions with
start_tool/startTooland record results. - Record complete token usage and a priceable model so token counts and cost render.
- View the resulting conversation, turns, and tool calls in the Conversations tab.
How the Agent Lens SDK works with agents
The Agent Lens SDK includes a generic OTel ingest system for agents, meaning that Agent Lens can capture information from any OTel span in your agent’s code. However, Agent Lens requires special handling of the following spans to render your agent’s traces in the Conversations tab of the Agent Lens UI.
In Python, all four functions work as context managers (
with tracing.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, and wrap each agent run in tracing.runIsolated() so that concurrent runs don’t share a conversation.
Other GenAI semantic-convention attributes, such as gen_ai.usage.* and gen_ai.agent.name, enable additional rendering, but they’re optional.
Prerequisites
- A CoreWeave Forge account and API key.
- An OpenAI API key.
- Python 3.9+ (for the Python examples).
- Node.js 18+ and a TypeScript runner such as
tsx(the TypeScript examples require built-infetchand aren’t plain JavaScript).
Install packages
Install the following packages into your developer environment:.mts files and run them with npx tsx [FILENAME].mts.
Initialize Agent Lens
tracing.init() authenticates with your API key and configures the OTel exporter that sends agent spans to Agent Lens. The project name must include your team. The SDK reads the API key from the WANDB_API_KEY environment variable, or you can pass it as the api_key / apiKey argument.
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 Agent Lens initialization in place, the next step combines them into a complete agent loop. This loop shows how conversations, turns, LLM calls, and tool calls nest together. The following example runs three turns in a single conversation. 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.
The Agent Lens SDK automatically traces calls made with the OpenAI, Anthropic, and Google Gen AI client libraries. This quickstart records each LLM call by hand with
start_llm() to show how the spans fit together, so it passes autopatch_integrations=False to init(). Without it, each call is recorded twice: once by your start_llm() span and once by the automatic integration. Pass the argument on your first init() call, because a later call with autopatch_integrations=False doesn’t remove patches that an earlier call applied. In your own code, either let autopatching record your LLM calls, or turn it off and record them yourself.Record token usage and cost
Eachchat span carries token usage and a model ID. Agent Lens renders token counts from usage and derives cost from usage plus the model ID, so an incomplete or unpriceable value shows 0 in / 0 out tokens or Cost - even when the rest of the trace looks correct. record(...) sets these fields (along with output_messages, response_id, reasoning, and more) in one call. Only the fields you pass are applied.
Two things must be right for cost to appear:
- Complete usage.
input_tokensis the total input, including any cached tokens. Agent Lens prices cache reads and cache writes at their own rates and subtracts them from the input total, socache_read_input_tokensandcache_creation_input_tokensmust be reported in addition to a totalinput_tokensthat includes them. For providers with prompt caching (for example, Anthropic), cached tokens routinely dominate the input, so omitting them makes usage and cost render as roughly zero. - A priceable model ID. Pass the concrete ID the response returns (
resp.model) asresponse_model. Cost is a lookup on the model. Agent Lens prefersresponse_model(the exact model the provider served) and falls back to themodelyou passed tostart_llm. An alias such asopusorsonnetis not priceable and rendersCost -.
prompt_tokens, so the example above maps directly. Anthropic reports cached tokens separately from input_tokens, so add them back into the total Agent Lens prices against:
See your agent traces in the Conversations tab
Open your project in Agent Lens and select Conversations. You’ll see:- One conversation for
research-botcontaining 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.
Link to a conversation from your app
To deep-link from your own UI to a conversation in Agent Lens, you need the conversation’s ID.start_conversation / startConversation exposes it as conversation_id / conversationId, so you can log it or store it alongside your own request ID and open the conversation later from the Conversations tab.
Next steps
- Learn how to trace agents with Agent Lens and what features and options are available in the Agent Lens SDK.
- See Choose an agent integration for more options about how to integrate Agent Lens with your agents.