Skip to main content

Run ledger

The run ledger answers "what did this run do and what did it cost" from one id. Give it a workflow run id, a workflow session id, an agent run id, a request id, a trace id or a session id, and it returns the model requests with their tokens and spend, MCP tool calls, tool calls the model made, guardrail decisions, audit rows, eval scores, shadow evaluation verdicts and online scores that belong to it, merged into one time-ordered list with totals. It only reads what the gateway already stores, so it works on runs that happened before you looked.

A real-world scenario​

Finance asks why one workflow run cost eleven dollars when its budget was two. The workflow owner looks the run id up in the run ledger and sees that one step retried four times against a rate limited agent, and that each attempt was a full model call. The ledger shows every request with its spend, so the owner can point at the exact step, and the JSON export goes into the ticket as evidence.

Step by step​

  1. Find the id. Copy the run id from the run drawer on Workflow Runs, or a request id or session id from Logs.

  2. In the Admin UI open Run Ledger under Agentic in the left nav (/ui/run-ledger), paste the id and click Look up.

  3. Read the Totals strip for spend, tokens and duration, then scan the entries table, which is sorted by time.

  4. Click Export JSON to download the ledger, or fetch it with curl:

    curl http://localhost:4000/run_ledger/run_01J... -H "Authorization: Bearer $SPANWAY_API_KEY"
    curl -OJ http://localhost:4000/run_ledger/run_01J.../export -H "Authorization: Bearer $SPANWAY_API_KEY"

Expected result​

The page and the API return the same ledger. kind says what the id turned out to be (for example workflow_run), totals sums the spend and tokens of every request, and entries lists each request, tool call, guardrail decision and workflow event in time order. The export is a JSON file named after the id. An id you may not read, or that retention already deleted, returns 404.

Reading a ledger​

curl http://localhost:4000/run_ledger/run_01J... -H "Authorization: Bearer $SPANWAY_API_KEY"
{
"id": "run_01J...",
"kind": "workflow_run",
"session_ids": ["ses_01J..."],
"workflow_run": {"run_id": "run_01J...", "session_id": "ses_01J...", "workflow_type": "release-notes", "status": "completed", "created_at": "2026-10-01T12:00:00Z", "updated_at": "2026-10-01T12:01:33Z"},
"lens": "not_found",
"truncated": false,
"totals": {"requests": 12, "mcp_calls": 3, "tool_calls": 5, "guardrail_decisions": 12, "guardrail_interventions": 1, "guardrail_flags": 0, "spend": 0.0841, "total_tokens": 48210, "models": ["claude-sonnet-5-5"], "duration_ms": 93400},
"entries": [
{"kind": "workflow_event", "at": "2026-10-01T12:00:00Z", "event_type": "step.started", "step_name": "collect-commits", "sequence_number": 0},
{"kind": "request", "at": "2026-10-01T12:00:01Z", "request_id": "chatcmpl-...", "model": "claude-sonnet-5-5", "total_tokens": 4120, "spend": 0.0071},
{"kind": "guardrail", "at": "2026-10-01T12:00:01Z", "request_id": "chatcmpl-...", "guardrail_name": "pii-mask", "status": "guardrail_intervened"}
]
}

GET /run_ledger/{id}/export returns the same ledger as a JSON file download, named after the id.

How the id is resolved​

The gateway tries each interpretation in order and uses the first that matches.

  1. A workflow run id or a workflow session id. The ledger covers the run's session, every session id recorded on the run's messages, and the run's event log.
  2. A request id. When the request was an agent call over A2A the ledger is an agent_run, otherwise it is a trace, and either way it covers every request that shares the request's trace.
  3. A trace or session id. Spend logs store the trace id and a client supplied session id in the same field, so the ledger reports trace when Lens also holds a trace with that id and session otherwise.

When Lens is connected, agent and tool spans from the matching Lens trace are added as trace_span entries, and any spend log a Lens span links to is included even when it was logged under another session. lens in the response is included, not_found or unavailable, and a Lens outage never fails the ledger.

What each entry comes from​

Entry kindSource
requestSpend logs, with model, provider, tokens, spend, status and cache hit
mcp_callSpend logs of MCP tool calls made through the gateway
tool_callThe tool call index the gateway writes for tool calls in model responses
guardrailThe guardrail decisions recorded on each request, without the guarded text
auditAudit log rows whose object is the run, its sessions or its requests, within an hour of the run
workflow_eventThe workflow run's event log
workflow_attemptEach attempt of a workflow step, with its step key, attempt number, status, error code and reported cost, so a retried step shows every try. Only with the workflow_history feature
eval_scoreEval results for dataset items that were drawn from one of the run's requests through an eval built from logs
shadow_verdictShadow evaluation verdicts on one of the run's requests, with the judge and shadow spend
online_scoreOnline scoring results on one of the run's requests, one per rule and scorer, with the score, the verdict, whether scoring failed and the judge spend. The judge's reason is not included, and scores of deleted rules are left out
trace_spanAgent and tool spans from Lens

workflow_run also says where a replay came from. replay_of_run_id and replay_from_step_key are set on a run that replays an earlier run from one step, and replays lists the replays made of this run with their status. These lineage fields and the workflow_attempt entries are filled only when the license includes the workflow_history feature, and stay empty otherwise.

Totals add up request and MCP spend and request tokens, count every entry kind, count failed requests, count guardrail interventions (guardrail_intervened, content blocked or changed) apart from guardrail flags (guardrail_flagged, a violation recorded while the content went through), average the eval scores and report the run's first start, last end and duration. The start and end cover the workflow run's own created and updated times, its events, requests, MCP calls, tool calls, guardrail decisions and Lens spans. Audit rows, eval scores, shadow verdicts and online scores are left out of that window because they are usually written after the run. Shadow evaluation and online scoring judge spend is reported separately as eval_spend because it was not spent by the run itself. A ledger holds at most 2,000 spend log rows, and truncated is true when a run has more.

Access​

A ledger that shows an agent's calls needs a Spanway enterprise license that includes the agent_governance feature and answers 403 without it, whether you ask for it by the agent run (A2A request) id, a session id, a trace id or a workflow run id. Ledgers for workflow runs, traces and sessions that hold no agent calls stay free. Step attempts and replay lineage need the workflow_history feature, and without it the rest of the ledger is still returned.

Proxy admins and admin viewers see every row. Other users see the requests they made and the requests of teams where they are a team admin or hold the spend logs permission, the same scope the Logs page uses, and only those requests' tool calls, guardrail decisions, eval scores, shadow verdicts and online scores. A workflow run's events are shown to admins and to the key that created the run. Audit rows are shown to proxy admins and admin viewers only. A key with no user attached cannot read ledgers. The routes are read-only and answer 405 to any other method. With custom roles both routes belong to the logs permission.

Admin UI​

The Run Ledger page under Agentic takes an id, shows the totals and the time-ordered entries, and downloads the JSON export. The Logs page links each trace or session to its ledger, the Workflow Runs drawer links each run, and the Agents page links to the ledger lookup.

Errors​

StatusMessageMeaning and fix
404No run found for id '<id>'Nothing you may read matches the id, or retention deleted it. Check the id, non admins only see their own and their teams' runs.
403Not allowed to view run ledgersThe key has no user and is not a proxy admin. Give the key a user, or use an admin key.
403{"error": "The 'agent_governance' feature needs a Spanway enterprise license that includes it."}The ledger includes agent calls and the license lacks the feature. This 403 carries an error object instead of plain text. Add the feature.
500DB not connected. ...The gateway has no database, so there are no rows to read. Set DATABASE_URL.

In the Admin UI open Run Ledger under Agentic, paste the id and click Look up. See the Run Ledger page.

Logs​

The ledger writes no log lines of its own. A Lens outage while the ledger is built shows as lens: unavailable in the answer and as the Trace read unavailable warning described on agent traces.

Audit​

Downloading a ledger with GET /run_ledger/{run_id}/export writes an exported row on Spanway_SpendLogs with run_ledger:<run id> as object_id and the ledger kind, entry count and truncated flag as its value, never the entries. The row also appears in the export ledger. Reading a ledger in the page or with GET /run_ledger/{run_id} writes nothing.