Logging Utilities
stirrup.utils.logging
Rich logging for agent workflows with visual hierarchy.
AssistantMessage
Bases: BaseModel
LLM response message with optional tool calls and token usage tracking.
ToolMessage
Bases: BaseModel
Tool execution result returned to the LLM.
UserMessage
Bases: BaseModel
User input message to the LLM.
AgentLoggerBase
Bases: ABC
Abstract base class for agent loggers.
Defines the interface that Agent uses for logging. Implement this to create custom loggers (e.g., for testing, file output, or monitoring services).
Properties are set by Agent after construction: - name, model, max_turns, depth: Agent configuration - finish_params, run_metadata, output_dir: Set before exit for final stats
__enter__
abstractmethod
__enter__() -> Self
__exit__
abstractmethod
__exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: object,
) -> None
Exit logging context. Called when agent session ends.
on_step
abstractmethod
Report step progress and stats during agent execution.
assistant_message
abstractmethod
assistant_message(
turn: int,
max_turns: int,
assistant_message: AssistantMessage,
) -> None
user_message
abstractmethod
user_message(user_message: UserMessage) -> None
task_message
abstractmethod
tool_result
abstractmethod
tool_result(tool_message: ToolMessage) -> None
context_summarization_start
abstractmethod
context_summarization_complete
abstractmethod
debug
abstractmethod
info
abstractmethod
warning
abstractmethod
AgentLogger
Bases: AgentLoggerBase
Rich console logger for agent workflows.
Implements AgentLoggerBase with rich formatting, spinners, and visual hierarchy. Each agent (including sub-agents) should have its own logger instance.
Usage
from stirrup.clients.chat_completions_client import ChatCompletionsClient
Agent creates logger internally by default
client = ChatCompletionsClient(model="gpt-4") agent = Agent(client=client, name="assistant")
Or pass a pre-configured logger
logger = AgentLogger(show_spinner=False) agent = Agent(client=client, name="assistant", logger=logger)
Agent sets these properties before calling enter:
logger.name, logger.model, logger.max_turns, logger.depth
Agent sets these before calling exit:
logger.finish_params, logger.run_metadata, logger.output_dir
Initialize the agent logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show_spinner
|
bool
|
Whether to show a spinner while agent runs (only for depth=0) |
True
|
level
|
int
|
Logging level (default: INFO) |
INFO
|
Source code in src/stirrup/utils/logging.py
__enter__
__enter__() -> Self
Enter logging context. Logs agent start and starts spinner if depth=0.
Source code in src/stirrup/utils/logging.py
__exit__
__exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: object,
) -> None
Exit logging context. Stops spinner and logs completion stats.
Source code in src/stirrup/utils/logging.py
on_step
Report step progress and stats during agent execution.
Source code in src/stirrup/utils/logging.py
set_level
set_level(level: int) -> None
is_enabled_for
debug
Log a debug message (dim style).
info
warning
Log a warning message (yellow style).
error
Log an error message (red style).
critical
Log a critical message (bold red style).
Source code in src/stirrup/utils/logging.py
exception
Log an error message with exception traceback (red style with traceback).
Source code in src/stirrup/utils/logging.py
assistant_message
assistant_message(
turn: int,
max_turns: int,
assistant_message: AssistantMessage,
) -> None
Log an assistant message with content and tool calls in a panel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
turn
|
int
|
Current turn number (1-indexed) |
required |
max_turns
|
int
|
Maximum number of turns |
required |
assistant_message
|
AssistantMessage
|
The assistant's response message |
required |
Source code in src/stirrup/utils/logging.py
user_message
user_message(user_message: UserMessage) -> None
Log a user message in a panel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_message
|
UserMessage
|
The user's message |
required |
Source code in src/stirrup/utils/logging.py
task_message
Log the initial task/prompt at the start of a run.
Source code in src/stirrup/utils/logging.py
warnings_message
Display warnings at run start as simple text.
Source code in src/stirrup/utils/logging.py
tool_result
tool_result(tool_message: ToolMessage) -> None
Log a single tool execution result in a panel with XML syntax highlighting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_message
|
ToolMessage
|
The tool execution result |
required |
Source code in src/stirrup/utils/logging.py
context_summarization_start
Log context window summarization starting in an orange panel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pct_used
|
float
|
Percentage of context window currently used (0.0-1.0) |
required |
cutoff
|
float
|
The threshold that triggered summarization (0.0-1.0) |
required |
Source code in src/stirrup/utils/logging.py
context_summarization_complete
Log the completed context summarization with summary content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
summary
|
str
|
The generated summary of the conversation |
required |
bridge
|
str
|
The bridge message that will be used to continue the conversation |
required |
Source code in src/stirrup/utils/logging.py
_aggregate_list
_aggregate_list(metadata_list: list[T]) -> T | None
Aggregate a list of metadata using add.
Source code in src/stirrup/core/models.py
aggregate_metadata
aggregate_metadata(
metadata_dict: dict[str, list[Any]],
prefix: str = "",
return_json_serializable: bool = True,
) -> dict | object
Aggregate metadata lists and flatten sub-agents into a single-level dict with hierarchical keys.
For entries with nested run_metadata (e.g., SubAgentMetadata), flattens sub-agents using dot notation. Each sub-agent's value is a dict mapping its direct tool names to their aggregated metadata (excluding nested sub-agent data, which gets its own top-level key).
At the root level, token_usage is rolled up to include all sub-agent token usage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata_dict
|
dict[str, list[Any]]
|
Dict mapping names (tools or agents) to lists of metadata instances |
required |
prefix
|
str
|
Key prefix for nested calls (used internally for recursion) |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
dict | object
|
Flat dict with dot-notation keys for sub-agents. |
|
Example |
dict | object
|
{
"token_usage": |
dict | object
|
} |
Source code in src/stirrup/core/models.py
_is_subagent_metadata
Check if data represents sub-agent metadata.
Sub-agent metadata can be: - A Pydantic SubAgentMetadata object with run_metadata attribute - A dict where all values are dicts/objects (from aggregate_metadata flattening)
Source code in src/stirrup/utils/logging.py
_format_token_usage
Format token_usage (dict or TokenUsage object) as a human-readable string.
Source code in src/stirrup/utils/logging.py
_get_nested_tools
Extract nested tools dict from sub-agent metadata.
Source code in src/stirrup/utils/logging.py
_add_tool_branch
Add a tool entry to the tree, handling nested sub-agent data recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent
|
Tree
|
The tree or branch to add to |
required |
tool_name
|
str
|
Name of the tool or sub-agent |
required |
tool_data
|
object
|
The tool's metadata (dict, Pydantic model, list, or scalar) |
required |
skip_fields
|
set[str]
|
Fields to skip when displaying dict contents |
required |