Display & logging¶
Live console output, structured logging, and fast trajectory stats. See the Display & logging guide.
Display¶
Display
¶
Display(mode: DisplayMode | str = SHORT)
Mode-aware console display for agent events.
Uses rich.text.Text objects (not markup strings) to avoid
escaping issues with model output containing [brackets].
Source code in caw/display.py
on_metadata
¶
Print metadata key-value pairs (agent, model, session, etc.).
Source code in caw/display.py
on_user_message
¶
Print the user's message.
Source code in caw/display.py
on_text
¶
on_text(block: TextBlock) -> None
Buffer an assistant text block (printed on next event or at turn end).
Source code in caw/display.py
on_thinking
¶
on_thinking(block: ThinkingBlock) -> None
Print a thinking block.
Source code in caw/display.py
on_tool_call
¶
on_tool_call(block: ToolUse) -> None
Print a tool call (args only — result not yet known).
Source code in caw/display.py
on_tool_result
¶
on_tool_result(block: ToolUse) -> None
Print a tool result (output now available on the block).
Source code in caw/display.py
on_turn_end
¶
on_turn_end(result: str, usage: UsageStats, duration_ms: int) -> None
Print end-of-turn stats or deferred result text.
Source code in caw/display.py
DisplayMode¶
DisplayMode
¶
Bases: str, Enum
Print modes for agent output.
Global display¶
get_global_display
¶
get_global_display() -> Display | None
Return the global Display. Falls back to CAW_LOG env var on first call.
Source code in caw/display.py
set_global_display
¶
set_global_display(display: Display | None) -> None
Set (or clear) the global Display instance.
AgentLogger¶
AgentLogger
¶
Bases: Protocol
Minimal logger surface used by caw.
FastStats¶
FastStats
dataclass
¶
FastStats(path: Optional[Path] = None, agent: str = '', model: str = '', session_id: str = '', created_at: str = '', completed_at: str = '', usage_limited: bool = False, duration_ms: int = 0, cost_usd: float = 0.0, input_tokens: int = 0, output_tokens: int = 0, cache_read_tokens: int = 0, cache_write_tokens: int = 0)
Lightweight statistics for a CAW trajectory.
The class is intentionally narrow: it exposes only the fields that
consumers ask for repeatedly without paying for a full trajectory parse.
For everything else (turns, tool calls, content blocks) load the file
via caw.agent.Session.load_trajectory instead.
All cost_usd / token values come from the trajectory's
total_usage (recursive across subagents) when present, falling back
to usage for older trajectories that did not record it separately.
to_dict
¶
from_trajectory
classmethod
¶
from_trajectory(trajectory: Trajectory, *, path: str | Path | None = None) -> FastStats
Build FastStats from an in-memory Trajectory.
Source code in caw/faststats.py
from_path
classmethod
¶
from_path(path: str | Path) -> Optional[FastStats]
Read fast stats from path.
Returns None if the file does not exist, is empty, or is not a
recognizable trajectory file. Tries the head/tail fast path first
and falls back to a full JSON parse on failure.
Source code in caw/faststats.py
iter_directory
classmethod
¶
iter_directory(directory: str | Path, *, patterns: Iterable[str] = ('**/trajectory.json', '**/*.traj.json'), skip_parts: Iterable[str] = ()) -> Iterator[FastStats]
Yield FastStats for every trajectory file under directory.
patterns is a list of globs (relative to directory) to scan;
the default catches both the canonical CAW layout
(sessions/<id>/trajectory.json) and the .traj.json files
produced by ad-hoc writers. Files whose path contains any directory
component listed in skip_parts are excluded. Unreadable or
malformed files are silently dropped.
Source code in caw/faststats.py
directory_total_cost
classmethod
¶
Sum cost_usd across every trajectory under directory.
Extra keyword arguments are forwarded to iter_directory.