Core

Trajectory

The ordered record of one agent run. Events, usage, errors, and the rules that keep it portable.

A trajectory is what an agent did: its input and output, and every event in between. Viewport Evals asserts on it, HITL adds approval events to it, Context adds skill_load events. The shape is trajectory/v1 (https://getviewport.com/schemas/trajectory/v1.json).

Event typeWhat it records
agentan agent or subagent invocation that parents other events
model_callone request/response round trip to a model, with usage
tool_callone tool invocation with args and a result or error
approvala HITL decision about a gated call
skill_load, skill_unload, context_assemblyContext loading skills and assembling a prompt
handoffcontrol passed between agents
messagea conversational message

Rules that keep trajectories portable between languages and versions:

  • Only events is required on input. Writers always emit $schema, id and started_at.
  • Events are in completion order. Parentage is parent_id, never position.
  • usage.input_tokens includes cache reads and writes (the OpenTelemetry convention).
  • A trajectory is a machine record: unknown fields and unknown event types are accepted and kept on round trip.
  • Durations are *_ms numbers; timestamps are RFC 3339 UTC with millisecond precision.
main.py
from viewportai.core import Trajectory, stable_json, summarize

t = Trajectory.model_validate(
    {
        "events": [
            {
                "type": "model_call",
                "model": "m",
                "usage": {"input_tokens": 812, "output_tokens": 64, "cost_usd": 0.0034},
            },
            {"type": "tool_call", "tool_name": "lookup_order", "args": {"order_id": "A-1001"}},
            {"type": "tool_call", "tool_name": "issue_refund", "error": {"type": "Timeout", "message": "slow"}},
        ]
    }
)
print(stable_json([c.tool_name for c in t.tool_calls()]))  # ["lookup_order","issue_refund"]
s = summarize(t)
print(s.model_calls, s.tool_calls, s.tool_errors, s.tokens, s.cost_usd)  # 1 2 1 876 0.0034
main.ts
import { stableJson, summarize, type Trajectory, toolCalls } from "@viewportai/core";

const t: Trajectory = {
  events: [
    { type: "model_call", model: "m", usage: { input_tokens: 812, output_tokens: 64, cost_usd: 0.0034 } },
    { type: "tool_call", tool_name: "lookup_order", args: { order_id: "A-1001" } },
    { type: "tool_call", tool_name: "issue_refund", error: { type: "Timeout", message: "slow" } },
  ],
};
console.log(stableJson(toolCalls(t).map((c) => c.tool_name))); // ["lookup_order","issue_refund"]
const s = summarize(t);
console.log(s.model_calls, s.tool_calls, s.tool_errors, s.tokens, s.cost_usd); // 1 2 1 876 0.0034

Budgets and counts

A Budget limits resources (tokens, input_tokens, output_tokens, cost_usd, latency_ms); a number means "at most". A Range constrains a count: 2 means exactly two, {min: 1, max: 3} an inclusive range. check_budget / checkBudget compares a budget with a summary and fails anything it cannot measure, such as cost when a model call has no price.

main.py
from viewportai.core import check_budget, format_range, in_range, stable_json

checks = check_budget({"tokens": 1000, "cost_usd": 0.01}, {"tokens": 1240})
print(stable_json([c.message for c in checks]))  # ["1240 exceeds 1000","not measured"]
print(str(in_range(2, {"min": 1, "max": 3})).lower(), format_range({"min": 1}))  # true >= 1
main.ts
import { checkBudget, formatRange, inRange, stableJson } from "@viewportai/core";

console.log(stableJson(checkBudget({ tokens: 1000, cost_usd: 0.01 }, { tokens: 1240 }).map((c) => c.message))); // ["1240 exceeds 1000","not measured"]
console.log(inRange(2, { min: 1, max: 3 }), formatRange({ min: 1 })); // true >= 1

On this page