This is an interactive notebook. You can run it locally or use the following links:
- Create a mock of a simple LLM call and response, and log it to Weave.
- Create a mock of a more complex LLM call and response, and log it to Weave.
- Run a sample lookup query on the logged traces.
View logged traces
To view all of the Weave traces created when you run the code in this guide, go to the Traces tab in your Weave project (specified by team_id\project_id), and select the name of the trace.
Before beginning, complete the prerequisites.
Prerequisites: Set variables and endpoints
Before logging traces, you must configure the Service API endpoints and authenticate with your W&B credentials. The following code sets the URL endpoints used to access the Service API:https://trace.wandb.ai/call/starthttps://trace.wandb.ai/call/endhttps://trace.wandb.ai/calls/stream_query
project_id: the name of the W&B project where you want to log your traces.team_id: your W&B team name.wandb_token: your W&B API key.
Simple trace
The following sections walk you through creating a simple trace:Start a simple trace
The following code creates a sample LLM callpayload_start and logs it to Weave using the url_start endpoint. The payload_start object mimics a call to OpenAI’s gpt-4o with the query Why is the sky blue?.
On success, this code outputs a message indicating that the trace started:
End a simple trace
To complete the simple trace, the following code creates a sample LLM callpayload_end and logs it to Weave using the url_end endpoint. The payload_end object mimics the response from OpenAI’s gpt-4o given the query Why is the sky blue?. The object is formatted so that pricing summary information and the chat completion are generated in trace view in the Weave dashboard.
On success, this code outputs a message indicating that the trace completed:
Complex trace
Now that you’ve logged a simple, single-step trace, the following sections walk you through creating a more complex trace with child spans, similar to a multi-operation RAG lookup:- Start a complex trace
- Add a child span for a RAG document lookup
- Add a child span for an LLM completion call
- End a complex trace
Start a complex trace
The following code demonstrates how to create a more complex trace with multiple spans. An example is a Retrieval-Augmented Generation (RAG) lookup followed by an LLM call. The first part initializes a parent trace (payload_parent_start) that represents the operation. In this case, the operation processes the user query Can you summarize the key points of this document?.
The payload_parent_start object mimics the initial step in a multi-step workflow, logging the operation in Weave using the url_start endpoint.
On success, this code outputs a message indicating that the parent call is logged:
Add a child span for a RAG document lookup
The following code demonstrates how to add a child span to the complex parent trace started in the previous step. This step models the RAG document lookup sub-operation in the workflow. The child trace is initiated with thepayload_child_start object, which includes:
trace_id: links this child span to the parent trace.parent_id: associates the child span with the parent operation.inputs: logs the search query, for example,"This is a search query of the documents I'm looking for."
url_start endpoint, the code outputs a message indicating that the child call was started and completed:
Add a child span for an LLM completion call
The following code demonstrates how to add another child span to the complex parent trace, representing an LLM completion call. This step models the LLM response generation based on document context retrieved in the previous RAG operation. The LLM completion trace is initiated with thepayload_child_start object, which includes:
trace_id: links this child span to the parent trace.parent_id: associates the child span with the parent workflow.inputs: logs the input messages for the LLM, including the user query and the appended document context.model: specifies the model used for the operation (gpt-4o).
payload_child_end object ends the trace by logging the LLM-generated response in the output field. The code also logs usage summary information.
On success, the code outputs a message indicating the LLM child span trace has started and ended:
End a complex trace
The following code demonstrates how to finalize the parent trace, marking the completion of the workflow. This step aggregates the results of all child spans (for example, RAG lookup and LLM completion) and logs the final output and metadata. The trace is finalized using thepayload_parent_end object, which includes:
id: theparent_call_idfrom the initial parent trace start.output: represents the final output of the workflow.summary: consolidates usage data for the workflow.prompt_tokens: total tokens used for all prompts.completion_tokens: total tokens generated in all responses.total_tokens: combined token count for the workflow.requests: total number of requests made (in this case,1).
Run a lookup query
With traces logged to Weave, you can use the Service API to query them programmatically. The following code demonstrates how to query the traces created in previous examples, filtering only for traces where theinputs.model field is equal to gpt-4o.
The query_payload object includes:
project_id: identifies the team and project to query.filter: ensures the query returns only trace roots (top-level traces).query: defines the filter logic using the$exproperator.$getField: retrieves theinputs.modelfield.$literal: matches traces whereinputs.modelequals"gpt-4o".
limit: limits the query to 10,000 results.offset: starts the query at the first result.sort_by: orders results by thestarted_attimestamp in descending order.include_feedback: excludes feedback data from the results.