Event System¶
The event system in Bedrock Swarm provides comprehensive tracking and monitoring of agent activities, tool executions, and system operations. It enables detailed tracing of operations and facilitates debugging and monitoring.
Class Documentation¶
System for tracking and managing events in the agent swarm.
This class provides: 1. Event creation and storage 2. Event querying and filtering 3. Event relationship tracking (parent/child) 4. Chronological event ordering
Source code in src/bedrock_swarm/events.py
Functions¶
create_event(type: EventType, agent_name: str, run_id: str, thread_id: str, details: Dict[str, Any], metadata: Optional[Dict[str, Any]] = None) -> str
¶
Create and record a new event.
| PARAMETER | DESCRIPTION |
|---|---|
type
|
Type of event
TYPE:
|
agent_name
|
Name of agent involved
TYPE:
|
run_id
|
ID of the run this event belongs to
TYPE:
|
thread_id
|
ID of the thread this event belongs to
TYPE:
|
details
|
Event-specific details
TYPE:
|
metadata
|
Optional additional metadata
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
ID of the created event |
Source code in src/bedrock_swarm/events.py
start_event_scope(event_id: str) -> None
¶
Start a new event scope.
This sets the current event as the parent for any new events until the scope is ended.
| PARAMETER | DESCRIPTION |
|---|---|
event_id
|
ID of the event to set as current
TYPE:
|
Source code in src/bedrock_swarm/events.py
end_event_scope() -> None
¶
get_events(run_id: Optional[str] = None, thread_id: Optional[str] = None, agent_name: Optional[str] = None, event_type: Optional[EventType] = None) -> List[Event]
¶
Get events matching the specified filters.
| PARAMETER | DESCRIPTION |
|---|---|
run_id
|
Optional run ID to filter by
TYPE:
|
thread_id
|
Optional thread ID to filter by
TYPE:
|
agent_name
|
Optional agent name to filter by
TYPE:
|
event_type
|
Optional event type to filter by
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[Event]
|
List of matching events in chronological order |
Source code in src/bedrock_swarm/events.py
get_event_chain(event_id: str) -> List[Event]
¶
Get the chain of events leading to the specified event.
This follows the parent_event_id links to build the chain.
| PARAMETER | DESCRIPTION |
|---|---|
event_id
|
ID of the event to get the chain for
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[Event]
|
List of events in the chain, from root to specified event |
Source code in src/bedrock_swarm/events.py
format_event(event: Event) -> str
¶
Format an event for display.
| PARAMETER | DESCRIPTION |
|---|---|
event
|
Event to format
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Formatted string representation of the event |
Source code in src/bedrock_swarm/events.py
format_event_chain(event_id: str) -> str
¶
Format a chain of events for display.
| PARAMETER | DESCRIPTION |
|---|---|
event_id
|
ID of the event to get the chain for
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Formatted string representation of the event chain |
Source code in src/bedrock_swarm/events.py
Event Types¶
The system supports these event types:
- Agent Events:
agent_start: Agent begins processingagent_complete: Agent completes processingmessage_sent: Message sent between agents-
message_received: Message received by agent -
Tool Events:
tool_start: Tool execution beginstool_complete: Tool execution completes-
tool_error: Tool execution error -
Run Events:
run_start: Run beginsrun_complete: Run completeserror: Error occurred
Event Structure¶
Each event follows this structure:
{
"id": "evt_123", # Unique event ID
"type": "agent_start", # Event type
"timestamp": "2024-02-29...", # ISO format timestamp
"agent_name": "calculator", # Name of agent involved
"run_id": "run_456", # ID of the run
"thread_id": "thread_789", # ID of the thread
"parent_event_id": "evt_122", # ID of parent event
"details": { # Event-specific details
"message": "Processing request",
"status": "active"
},
"metadata": { # Additional metadata
"version": "1.0",
"environment": "prod"
}
}
Usage Examples¶
from bedrock_swarm.agency import EventSystem
# Create event system
events = EventSystem()
# Record an event
event_id = events.record_event(
type="agent_start",
agent_name="calculator",
run_id="run_123",
thread_id="thread_456",
details={"message": "Starting calculation"}
)
# Get event chain
chain = events.get_event_chain(event_id)
print(chain) # Shows event and all related events
# Filter events
agent_events = events.filter_events(
agent_name="calculator",
event_type="tool_start"
)
# Get event trace
trace = events.get_trace(run_id="run_123")
print(trace) # Shows complete trace of run
Event Chains¶
Events can form parent-child relationships:
- Parent Events:
- Agent start events
- Run start events
-
Tool start events
-
Child Events:
- Tool execution events
- Message events
- Completion events
Event Filtering¶
The system supports filtering by:
- Event type
- Agent name
- Time range
- Run ID
- Thread ID
Implementation Details¶
The event system provides:
- Event Recording:
- Unique ID generation
- Timestamp management
- Parent-child linking
-
Metadata attachment
-
Event Retrieval:
- Chain reconstruction
- Trace generation
- Event filtering
-
Chronological ordering
-
Event Analysis:
- Duration calculation
- Success rate tracking
- Performance monitoring
- Error pattern detection
Best Practices¶
-
Event Recording:
# Record start of operation start_id = events.record_event( type="operation_start", details={"operation": "calculation"} ) try: # Perform operation result = perform_calculation() # Record success events.record_event( type="operation_complete", parent_id=start_id, details={"result": result} ) except Exception as e: # Record error events.record_event( type="error", parent_id=start_id, details={"error": str(e)} ) -
Event Monitoring:
-
Performance Analysis: