The question after an agent incident is rarely "did it run?" It is "which user's request led to this command, in which sandbox, what did it return, and what did the agent do next?" If your logs are free-text lines like running tests for user 42, answering that means grepping and guessing.

Structured logs, one JSON object per event with consistent field names, turn those questions into queries. This post proposes a schema for agent runs, explains each field, lists what to keep out of logs, and shows a small TypeScript wrapper that emits it.

Three layers of identity

An agent run touches three layers, and your logs need an ID for each so you can join them:

  1. The request. A user asked for something. Give it a trace_id at the edge and pass it through everything that follows.
  2. The agent's step. The model decided to call a tool. Log the tool_call_id your model provider gives you, so a log line maps back to a specific turn in the transcript.
  3. The execution. The tool ran a command in a sandbox. Log the sandbox_id and the platform's own run ID, so you can look the run up on the platform side.

With all three, you can start from any end: a suspicious command, a user complaint or a model transcript, and walk to the others.

The schema

Every event carries:

Field Example meaning
ts ISO 8601 timestamp, UTC
event run.start, run.end, run.error, tool.call, approval.requested, and so on
trace_id The user request this belongs to
span_id This specific operation
tenant The customer, workspace or user the work is for
agent Which agent and version produced the call
tool_call_id The model's tool call, for joining with transcripts
sandbox_id Where it ran

A run's end adds run_id, exit_code, timed_out, oom_killed, duration_ms, and output sizes. Errors add the HTTP status, the platform's error code and its request_id.

What not to log

Logs are copied, shipped and retained more widely than almost any other data you have, so be stingy:

  • No secrets. Not in commands, environment dumps or error messages. If a command line might contain a token, log a hash of it, not the text.
  • No raw output by default. Command output can contain anything the program read: customer data, credentials, file contents. Log its size, and a hash if you need to spot repeats. Keep full output in a store with tighter access and a shorter retention, if at all.
  • No full prompts by default. Prompts often contain user data. Log the prompt template version instead, and keep transcripts in their own system with their own access rules.
  • Argument vectors with care. The program name (argv[0]) and the argument count are safe and useful. The full argument list may not be; a hash lets you group identical commands without storing them.

The wrapper

This wraps run from @sandscale/sdk 1.0.0 and writes JSON lines to stdout, where most log pipelines pick them up.

import { createHash, randomUUID } from 'node:crypto'; import { Sandscale, SandscaleError } from '@sandscale/sdk'; type Context = { traceId: string; tenant: string; agent: string; toolCallId: string }; const sha256 = (s: string) => createHash('sha256').update(s).digest('hex').slice(0, 16); function log(event: string, fields: Record<string, unknown>) { process.stdout.write(JSON.stringify({ ts: new Date().toISOString(), event, ...fields }) + '\n'); } export async function loggedRun(sandscale: Sandscale, ctx: Context, sandboxId: string, region: string | undefined, argv: string[], timeoutSeconds: number) { const span = randomUUID(); const base = { trace_id: ctx.traceId, span_id: span, tenant: ctx.tenant, agent: ctx.agent, tool_call_id: ctx.toolCallId, sandbox_id: sandboxId }; log('run.start', { ...base, argv0: argv[0], argc: argv.length, argv_sha: sha256(JSON.stringify(argv)), timeout_s: timeoutSeconds }); try { const r = await sandscale.sandboxes.run(sandboxId, argv, { region, timeoutSeconds }); log('run.end', { ...base, run_id: r.runId, exit_code: r.exitCode, timed_out: r.timedOut, oom_killed: r.oomKilled, duration_ms: r.durationMs, stdout_bytes: Buffer.byteLength(r.stdout), stderr_bytes: Buffer.byteLength(r.stderr), stderr_tail_sha: sha256(r.stderr.slice(-2000)), }); return r; } catch (err) { const e = err instanceof SandscaleError ? { status: err.status, code: err.code, request_id: err.requestId } : { code: 'client_error' }; log('run.error', { ...base, ...e }); throw err; } } 

A few choices to note:

  • Hashes, not content. argv_sha groups identical commands, and stderr_tail_sha spots the same failure repeating across runs, without storing either. The hashes are truncated to 16 hex characters; that is plenty for grouping, though not for anything security-sensitive.
  • Errors keep the request ID. The SDK's SandscaleError carries status, code and requestId. The request ID is what we need from you to trace a problem on our side, and the code is what your dashboards should group by.
  • The error is rethrown. Logging never swallows a failure.

Label sandboxes, too

Logs are one side. The other is making the platform's records easy to join with yours. When you create a Sandscale sandbox you can pass labels, a map of key-value strings, and name. Put your trace_id and tenant in the labels, and a sandbox you find running at 3 a.m. tells you where it came from.

On the platform side, every run is recorded. GET /api/v1/sandbox/runs/{id} returns a run's details and cost, per the sandbox API guide, and the run record in the API reference includes the command, its status and the instance it ran in. Separately, the hash-chained audit log records who started and stopped sandboxes, which commands ran and which secrets were used. Your structured logs answer "why did our system do this?"; those records answer "what happened on the platform?". You want both.

Queries this makes easy

With the schema above in any log store that can query JSON fields:

  • All runs for one user request: filter on trace_id.
  • Which agent version produces the most timeouts: group run.end by agent where timed_out is true.
  • Whether a failure is new: count stderr_tail_sha values over the last day against the last week.
  • Which tenant is using the most sandbox time: sum duration_ms by tenant.
  • Everything that happened in one sandbox: filter on sandbox_id, then look up each run_id.

Retention and access

Decide retention before you need it. A common split: structured event logs for months, because they are small and safe; full output and transcripts for days, behind narrower access. Write it down in the same place as your incident runbook, because the first question in an incident is how far back you can see.

Originally published on the Sandscale blog.