Skip to main content
For the complete documentation index for agents and LLMs, see llms.txt.

Session Readers for Code MetricsBeta

Beta
This is coming — this page describes a feature that's still being rolled out and may not be available to you yet.

Code metrics read turns, answers, documents, tools, and costs through ready-made helpers.


A code metric reads a session through helpers you import from dc_haystack_utilities.evaluation. The helpers let you score the answer a RAG pipeline gave and the context it was given, without parsing a trace yourself. Tracing of content can be disabled per Run, so every reader returns an empty value when the data is missing.

Reach Turns by Number​

Turns are numbered from 1, the same way as the focus argument of your function and turn/<n> result addresses.

  • turns(session, focus=None) returns a list of (n, trace) pairs for the turns your function is asked about. Without focus, it returns every turn.
  • turn(session, n) returns the trace of one turn.

If you ask for a turn the session doesn't have, both helpers raise NoSuchTurn, and the message lists the turns that exist. This way, a wrong turn number fails visibly instead of scoring an empty turn. Every other reader takes the trace of one turn.

Read the Answer and Its Context​

To judge whether an answer is faithful to what the pipeline retrieved, read both from the same trace:

  • get_last_reply(trace) returns the answer: the reply of the last LLM that answered. For an Agent, that is its final step. For a pipeline without an Agent, it's the last chat generator. It returns an empty string when nothing replied or the last step only called tools.
  • get_intermediate_replies(trace) returns the replies before the answer, such as an Agent's "Let me look that up." before it calls a tool.
  • get_last_context(trace) returns everything the answering LLM was given. By default it keeps only user messages and tool results, so system prompts and the LLM's own earlier replies don't count as context. Pass roles= to change which message roles it keeps.

Read Documents, Tools, and Skills​

  • documents(trace) returns the documents that retrievers and rankers produced. By default, each document is listed once, matched by id, in its last recorded version. A reranked document therefore carries the ranker's score.
  • tool_calls(trace) returns the tool calls of the turn.
  • available_tools(trace) returns the tools the Agent could use.
  • available_skills(trace) returns the skills the Agent could load.

Limit Readers to Some Components​

replies, token_usage, models, and the readers above take a components= filter with one component name or several. A span matches when its own component name or the name of any component it ran inside is in the filter. An Agent's name therefore covers its LLM steps and tool calls. Use the filter to read one part of a pipeline, for example the tokens one Agent used, or the answer of one LLM when another LLM runs after it.

Read the Cost of a Turn​

To hold a pipeline to a budget, read what each turn cost, in US dollars. Each turn's trace carries trace["platform_cost"], the cost the platform recorded for that turn, so your code never computes prices itself. It has these keys:

  • status: Whether the turn is priced. It is priced, unpriced, not_recorded, or pending.
  • total_cost: What the turn cost. Read it only when status is priced.
  • by_component: The cost of each component, as entries with component and cost.
  • by_model: The cost of each model, as entries with model and cost.
  • unpriced_models: The models that have no price, when status is unpriced.

The platform prices a turn about a minute after it ran. When an evaluation reads a turn that isn't priced yet, the platform prices it first.

Built-In Cost Templates​

Three built-in Evaluator templates turn the cost into a score where lower is better. Use them as the Evaluator of an Evaluation goal, for example "no turn costs more than $1". Goals on these templates default to the Cost category.

  • max_turn_cost: What the most expensive turn of the conversation cost. Use it to cap the cost of any single turn.
  • mean_turn_cost: What a turn cost on average. Use it to cap the typical cost of a turn.
  • session_cost: What the whole conversation cost, all turns added up. Use it to cap the cost of a conversation.

The rationale lists each turn's cost and the component and model that took most of the costliest turn's spend.

When a Cost Metric Is Not Applicable​

The cost templates need every turn of a conversation priced. If any turn is not priced, the whole conversation is not applicable, so spend nobody measured never passes a budget. Those conversations leave the goal's denominator. A turn is not priced in these cases:

  • It used a model that has no price. Add a price override for the model.
  • It was imported from another workspace, so its cost was not recorded. Imported turns can't be priced.
  • Its pricing hasn't finished. Evaluate again later.

A cost result is frozen in its Evaluation Run. After you add a price override, run the evaluation again and choose to re-run everything (RERUN_ALL), so the new price applies to earlier conversations.

Update Existing Code Metrics​

documents_for_turn, tool_calls_for_turn, and context_for_turn are no longer available in dc_haystack_utilities.evaluation. A metric that imports them fails with an ImportError. Use documents, tool_calls, and get_last_context on turn(session, n) instead. To read turns, use turns(session, focus) or turn(session, n) instead of traces(session)[n - 1].