Canonical RuntimeRunHandle lifecycle: drive a streaming task through
runAgentTaskStream, observe llm_call events into a cost ledger, and
persist a RuntimeRunRow to your durable store via a single
RuntimeRunPersistenceAdapter.upsert(row) method.
pnpm tsx examples/runtime-run/runtime-run.tsstartRuntimeRun({ workspaceId, sessionId, taskSpec, adapter })to open a runhandle.observe(event)per yieldedRuntimeStreamEventto keep the cost ledger in sync (onlyllm_callevents contribute; everything else is a no-op so you can pipe the whole stream throughobserve)handle.complete({ status, resultSummary, error? })exactly once at end-of- stream (idempotent for the same status, throws for status transitions)handle.persist()to write aRuntimeRunRowvia yourRuntimeRunPersistenceAdapter(D1, postgres, KV — anything with anupsert(row))handle.cost()returns the accumulated{ tokensIn, tokensOut, costUsd, wallMs, llmCalls }for cost dashboards