How agents and applications consume SessionStore data
Four read paths
|
Task |
Read first |
Expand further |
Correct statistical boundary |
|
Look up the question and answer of a turn |
input/output of the latest trajectory |
Quality fields and source references |
The selected snapshot of a logical trajectory |
|
Replay models, tools, and agents |
atif_content |
steps, observation, embedded trajectories, and the relationship index |
Each real node in the current snapshot |
|
Summarize session usage |
Session turns and totalTokens |
snapshot.model_calls and cost status |
The latest snapshot of each turn, without historical copies |
|
Build evaluation and regression samples |
Q&A and the context required by the task |
Source identifiers, quality information, and tool results |
A well-defined turn or a complete business session |
Parsing order
-
Parse the records returned by the query. If you use the Session SQL, first confirm that
aggregation_erroris empty, and then parsesession_json. -
Strictly convert numeric fields to integers or Decimal values, and keep empty strings as missing values. Compare process_time values as exact integers.
-
Decode only the JSON text that is specified in the field definitions, such as atif_content, model_calls, and trajectory_relationships. Do not automatically parse plain text.
-
Read the root steps based on the ATIF schema_version, and recursively traverse subagent_trajectories. An empty steps array in a synthetic root does not mean that the entire execution has no content.
-
Locate a trajectory by trajectory_id, and locate a step by the combination of the current snapshot, trajectory_id, and step_id. Step numbers and JSON paths are not guaranteed to be stable across snapshots.
View the complete details of a turn:
import json
from pathlib import Path
from scripts.session_view import select_latest
rows = json.loads(Path("examples/trajectory-history.json").read_text())
latest = select_latest(rows, "session-demo-001", "otel_trace")
row = next(r for r in latest if r["trace_id"] == "11111111111111111111111111111111")
atif = json.loads(row["atif_content"])
print(row["input"], row["output"])
for step in atif["steps"]:
print(step["step_id"], step["source"], step["message"])
A complete recursive traversal can follow the storage order of the current ATIF:
def walk_trajectories(root):
yield root
for child in root.get("subagent_trajectories", [ ]):
yield from walk_trajectories(child)
The traversal is used to read content. Confirm the actual parent-child relationships by using parent_trajectory_id and delegation references. Multiple real roots under a synthetic root are independent of each other. Do not add delegation edges based on array containment or the Raw parentSpanId.
Q&A retrieval and session browsing
Read the outer input/output of a trajectory to quickly display the question and the current answer of the turn. If you need multiple turns, first generate a session:
python3 scripts/session_view.py examples/trajectory-history.json \
--session-id session-demo-001 --source-kind otel_trace
The command outputs two turns and 205 tokens. The output of a session is a summary of the answer in the most recent turn and does not indicate that the business task succeeded. If you obtain only a time slice, inform users of the coverage scope. Do not claim that a session is complete just because turns is greater than 0.
Model usage and estimated costs
Before you calculate statistics, you must select the latest snapshot of each logical trajectory. Then, use the outer totals or the model_calls values grouped by model. Do not additionally add up ATIF steps, final_metrics, or node copies in the display layer.
from scripts.session_view import model_usage_complete
for row in latest:
if not model_usage_complete(row):
print(row["trajectory_id"], "Model input or output usage is not fully reported")
This check verifies the usage coverage of the model calls that are included in the statistics. It cannot detect calls that were not collected at all. The tokensComplete field of a session indicates only that a total_tokens value exists for each turn. The two have different meanings.
Use a cost as a complete estimate only when cost.status=complete and total_cost has a value. If the status is partial, you can report only the known subtotal and must not use it as the total cost:
from decimal import Decimal
import json
known_costs = [ ]
costs_complete = True
for row in latest:
cost = json.loads(row.get("trajectory_extensions") or "{}").get("cost", {})
amount = row.get("total_cost", "")
if cost.get("status") != "complete" or amount == "":
costs_complete = False
continue
known_costs.append(Decimal(amount))
session_cost_cny = sum(known_costs, Decimal("0")) if costs_complete else None
Cached token hits are a subset of the input, and reasoning tokens are a subset of the output. When the same historical messages enter a real model request again, the input tokens that are actually reported by the request are still billed. Do not deduct them just because the context is copied.
Troubleshoot tools and data quality
Find calls in the tool_calls of ATIF, and then associate the results by using observation.results[ ].source_call_id. Handle missing results based on the quality information. Do not assume that all tools succeeded just because tool_errors=0.
For a trace, you can look up the source records by using the outer trace_id and the node's extra.otel_trace.span_id. This span_id may point to an agent execution or a model call. When you look it up, read its operation type and check the source, time, and raw records.
degraded describes the quality of observable data. Tool or business failures, restricted model output, and missing samples are different issues. A single source stop, an HTTP 200 response, or a completed status that is marked by the application cannot prove that an answer is correct.
Build evaluation or regression samples
First, define the sampling granularity based on the task: single-turn Q&A, tool calls, agent executions, or multi-turn tasks. The existence of trajectory data alone does not mean that downstream evaluation, training, or experience feedback has been automatically performed.
A minimal single-turn sample can store the question, the answer, and source references:
samples = [ ]
for row in latest:
if row.get("degraded") != "0" or not row.get("input") or not row.get("output"):
continue
samples.append({
"question": row["input"],
"answer": row["output"],
"source": {
"source_kind": row["source_kind"],
"trajectory_id": row["trajectory_id"],
"process_time": row["process_time"]
}
})
This is a basic filtering example. In practice, you should also check the degree of completion, expected answers, tool results, and context requirements based on the task. degraded=0 is not a label for a correct answer, and an empty result does not automatically constitute a bad case.
When you display "newly added executions in this run", you can filter out copied historical steps. When you evaluate the context that the model actually received, keep the required history. Context retention, deduplication, and supervision masks for training data depend on the specific task. Do not globally delete copied context or treat deterministic scheduling as model-generated answers.
Multi-turn samples should store the related conversationTurns, the latest snapshot of each turn, and the coverage scope. A partial session that is captured by using a time window can be used for local analysis, but must not be used as a success or failure label for a complete task.