Skip to content

Runner Interface ​

All agentrunner libraries implement a common, language-agnostic Runner interface. Each language expresses this interface idiomatically, but the conceptual shape is the same.

Runner ​

A Runner executes prompts against an AI coding agent and returns results. The primary primitive is Start, which returns a Session object. Run and RunStream are convenience wrappers over Start.

Runner:
  Start(prompt, options)     → Session
  Run(prompt, options)       → Result
  RunStream(prompt, options) → Stream<Message>
  • Start launches an agent process and returns a Session for full lifecycle control.
  • Run starts a session, drains all messages internally, and returns the final Result.
  • RunStream starts a session and returns the messages iterable.

Session ​

A Session encapsulates a running agent process.

Session:
  messages → Stream<Message>   — iterable of messages as they arrive
  result   → Result            — blocks/resolves when the agent finishes
  abort()  → void              — terminates the agent process
  send()   → error             — send a message to the running agent (requires channelEnabled)

See the Channels guide for details on two-way communication.

LanguageMessagesResultAbort
Go<-chan MessageResult() (*Result, error)Abort()
TypeScriptAsyncIterable<Message>Promise<Result>abort(): void
Pythonasync for msg in sessionawait session.resultsession.abort()

Sessions are safe to abandon — abandoning a session terminates the underlying process.

Common Options ​

Every runner accepts these options. All are optional.

FieldTypeDescription
modelstringModel name or alias
systemPromptstringSystem prompt override
appendSystemPromptstringAppended to the default system prompt
workingDirstringWorking directory for the subprocess
envmap[string]stringAdditional environment variables
maxTurnsintMaximum number of agentic turns
timeoutdurationOverall execution timeout
skipPermissionsboolBypass interactive permission prompts

Each runner extends these with runner-specific options. See the individual runner pages for details.

Result ​

Returned by Run and available via Session.Result().

FieldTypeDescription
textstringFinal response text
isErrorboolWhether the run ended in error
exitCodeintProcess exit code (CLI runners) or 0 (API runners)
usageUsageToken counts
costUSDfloatEstimated cost in USD (0 for local runners)
durationMsint64Wall-clock duration in milliseconds
sessionIDstringSession identifier for resumption

Usage ​

FieldTypeDescription
inputTokensintPrompt / input tokens consumed
outputTokensintCompletion / output tokens generated
cacheCreationInputTokensintTokens written to cache (if supported)
cacheReadInputTokensintTokens read from cache (if supported)

Message ​

The unit of streaming output. Each message has a type and carries content or metadata.

FieldTypeDescription
typestringMessage type
rawbytesOriginal JSON for runner-specific parsing

Message Types ​

TypeDescriptionWhen emitted
systemInitialization metadata (model, session ID, tools)Start of stream
assistantModel-generated content (text, thinking, tool calls)During generation
tool_useThe model is invoking a toolDuring agentic turns
tool_resultOutput from a tool executionAfter tool execution
resultFinal result with text, usage, cost, durationEnd of stream
errorError or warningAny time
channel_replyReply from the agent to a channel messageDuring generation

Typed Accessors ​

AccessorAvailable onReturns
Text()assistant, resultThe text content
Thinking()assistantThinking/reasoning content
ToolName()tool_useName of the tool being called
ToolInput()tool_useTool call arguments
ToolOutput()tool_resultTool execution output
IsError()error, resultWhether this is an error
ErrorMessage()errorError description

Error Handling ​

Each language uses its native error conventions. All implementations distinguish these categories:

CategoryDescription
NotFoundRunner binary or API endpoint not found
TimeoutExecution exceeded the timeout
NonZeroExitCLI process exited with non-zero code
ParseErrorFailed to parse runner output
CancelledExecution was cancelled by the caller