Principle:24mlight A Share investment Agent Agent Monitoring Decorator
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Software_Engineering, Observability, AI_Agents |
| Last Updated | 2026-10-10 14:00 GMT |
Overview
A decorator-based monitoring pattern that wraps agent functions to provide automatic state tracking, stdout/stderr capture, log storage, and LLM interaction recording without modifying agent logic.
Description
The @agent_endpoint decorator transforms a plain agent function into a fully monitored component. When applied, it:
- Registers the Agent: Calls
api_state.register_agent(name, description)to register the agent in the global API state.
- State Tracking: Before execution, sets agent state to "running". After completion, sets state to "completed". On exception, sets state to "error".
- Input/Output Serialization: Serializes the input and output AgentState using
serialize_agent_stateand stores it inapi_state.
- Stdout/Stderr Capture: Redirects
sys.stdoutandsys.stderrto StringIO buffers during execution, capturing all terminal output for debugging.
- LLM Interaction Logging: The companion
log_llm_interaction(state)decorator factory wraps LLM calls to record request/response pairs, caller information, and timestamps.
- Trace Logging: When
trace_diris set in metadata, writes per-agent trace files for backtesting audit trails.
- Log Storage: Creates
AgentExecutionLogentries and stores them inBaseLogStoragefor the /logs API endpoint.
Usage
Use this principle when:
- Building multi-agent systems where observability and audit trails are critical
- Requiring automatic state tracking without modifying agent business logic
- Capturing LLM request/response pairs for debugging and compliance
Theoretical Basis
# Abstract decorator pattern (not real implementation)
@agent_endpoint("agent_name", "description")
def my_agent(state: AgentState) -> dict:
# Agent logic here
return {"messages": [...], "data": {...}, "metadata": {...}}
# What the decorator does:
# 1. api_state.register_agent("agent_name", "description")
# 2. api_state.update_agent_state("agent_name", "running")
# 3. Capture stdout/stderr
# 4. result = agent_func(state)
# 5. Serialize and store input/output
# 6. api_state.update_agent_state("agent_name", "completed")
# 7. Create and store AgentExecutionLog
LLM interaction logging:
# Decorator factory pattern
logged_call = log_llm_interaction(state)(get_chat_completion)
result = logged_call(messages)
# Records: caller info, messages, model, response, timestamp
Related Pages
Implemented By
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment