observe
constobserve:object
Type Declaration
Section titled “Type Declaration”duration
Section titled “duration”duration: () =>
Middleware=observeDuration
Turn duration timing → state[‘observe:duration’].
Creates an observe.duration() middleware that measures the wall-clock
duration of each turn in milliseconds.
Uses last-write-wins semantics (no reducer), so ctx.state['observe:duration']
always reflects the duration of the most recently completed turn.
Returns
Section titled “Returns”Middleware that tracks turn duration
Example
Section titled “Example”agent.use(observe.duration())
const result = await agent.run("Hello").resultconst ms = result.state['observe:duration'] as numberconsole.log(`Turn took ${ms}ms`)log: (
opts?) =>Middleware=observeLog
Structured JSON logging.
Creates an observe.log() middleware that emits structured JSON log events
for every lifecycle phase.
Logs session, turn, model, and tool start/end events as LogEvent objects.
By default, writes JSON lines to stderr — suitable for structured logging
pipelines (Datadog, Grafana, ELK, etc.).
Parameters
Section titled “Parameters”ObserveLogOptions
Optional configuration with custom output function
Returns
Section titled “Returns”Middleware that logs all lifecycle events
Example
Section titled “Example”// Default: JSON lines to stderragent.use(observe.log())
// Custom output (e.g., pino):agent.use(observe.log({ output: (event) => pino.info(event) }))tools: () =>
Middleware=observeTools
Tool call recording → state[‘observe:tools’].
Creates an observe.tools() middleware that records every tool execution
in the session, including arguments, results, duration, and errors.
Each tool call is appended to the observe:tools state array via a
reducer. The full history of tool calls is available in
ctx.state['observe:tools'] at any point during the session.
Returns
Section titled “Returns”Middleware that records tool call history
Example
Section titled “Example”agent.use(observe.tools())
const result = await agent.run("Search for cats").resultconst calls = result.state['observe:tools'] as ToolCallRecord[]for (const call of calls) { console.log(`${call.name}: ${call.duration}ms`)}usage: () =>
Middleware=observeUsage
Token usage tracking → state[‘observe:usage’].
Creates an observe.usage() middleware that accumulates token usage
across all model calls in a session.
Tracks inputTokens and outputTokens via a reducer that sums deltas
from each model response. The accumulated totals are available in
ctx.state['observe:usage'] at any point during the session.
Returns
Section titled “Returns”Middleware that tracks cumulative token usage
Example
Section titled “Example”agent.use(observe.usage())
const result = await agent.run("Hello").resultconst usage = result.state['observe:usage'] as Usageconsole.log(`Tokens: ${usage.inputTokens} in, ${usage.outputTokens} out`)