Skip to main content

Observability middleware for multi-agent LangGraph/LangChain workflows. Track agent trees, emit events, and trace execution across nested agents.

Project description

multiagent-trace-middleware

Real-time tracing middleware for Python agents. Build terminal UIs or desktop apps that display your LangGraph/LangChain agent execution with hierarchical visualization, color-coded agents, and live status updates.

Main Agent View Multi-Agent Hierarchy

What is this for?

When building apps that integrate Python AI agents (LangGraph, LangChain, etc.), you often need to display what your agent is doing in real-time. This middleware hooks into your agent and emits structured events that your UI can consume to show:

  • Which agent is currently running (with customizable colors for each agent)
  • Hierarchical nesting levels (main agent → subagent → sub-subagent)
  • Tool calls and their results (with arguments and completion status)
  • Agent thinking/reasoning in real-time
  • Todo list updates as agents work through tasks

Perfect for building:

  • Terminal-based agent UIs (like the screenshots above)
  • Desktop applications with agent monitoring
  • Web dashboards showing agent execution
  • Debugging tools for multi-agent systems

Features

  • Agent Hierarchy Tracking: Automatically tracks parent-child relationships between agents
  • Event Emission: Emits structured events for thinking, tool calls, tool results, and completions
  • Dual Output: Supports both EventBus (programmatic) and JSON stdout (terminal apps)
  • Color Support: Customizable foreground/background colors per agent for UI display
  • Level Tracking: Automatic nesting level calculation (0=root, 1=subagent, etc.)

Installation

pip install multiagent-trace-middleware

Or with uv:

uv add multiagent-trace-middleware

Quick Start

from multiagent_trace_middleware import AgentObserverMiddleware, get_event_bus

# Add middleware to your main agent
middleware = [
    AgentObserverMiddleware(
        "main",
        fg_color="#FFFFFF",
        bg_color="#1E88E5"
    ),
]

# Add middleware to subagents
subagent_middleware = [
    AgentObserverMiddleware(
        "researcher",
        fg_color="#000000",
        bg_color="#4CAF50"
    ),
]

# Subscribe to events
def on_event(event_type, agent_name, fg_color, bg_color, level, data):
    print(f"[L{level}] {agent_name}: {event_type}")

get_event_bus().subscribe(on_event)

Event Types

Event Type Description
agent_change Emitted when switching between agents
thinking AI reasoning/content output
tool_call Tool invocation with arguments
tool_result Tool execution results
completion Agent completion messages
todos Todo list updates
message Raw message objects (EventBus only)

JSON Output Format

When emit_json=True (default), events are written to stdout as JSON:

{
  "type": "tool_call",
  "timestamp": "2024-01-15T10:30:00.000000",
  "agent_name": "researcher",
  "agent_level": 1,
  "agent_fg_color": "#000000",
  "agent_bg_color": "#4CAF50",
  "tool_name": "search",
  "tool_call_id": "call_abc123",
  "args": {"query": "example"}
}

API Reference

AgentObserverMiddleware

AgentObserverMiddleware(
    name: str,                    # Agent name for display
    fg_color: str = "#000000",    # Foreground color (hex)
    bg_color: str = "#ffffff",    # Background color (hex)
    emit_json: bool = True,       # Emit JSON to stdout
    emit_events: bool = True,     # Emit to EventBus
)

EventBus

from multiagent_trace_middleware import get_event_bus

bus = get_event_bus()
bus.subscribe(callback)      # Subscribe to events
bus.unsubscribe(callback)    # Unsubscribe
bus.clear()                  # Clear all listeners

Agent Tree

from multiagent_trace_middleware import get_agent_tree, reset_agent_tree

tree = get_agent_tree()
node = tree.current          # Get current agent node
level = tree.current_level   # Get current nesting level
reset_agent_tree()           # Reset tree (useful for testing)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

multiagent_trace_middleware-0.1.0.tar.gz (202.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

multiagent_trace_middleware-0.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file multiagent_trace_middleware-0.1.0.tar.gz.

File metadata

File hashes

Hashes for multiagent_trace_middleware-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ea667b2d88bfaf7fa26f6118907e909f01c2111a07d12489eef4ca1b2e0fa7d0
MD5 dde5e7459c722215cc28efd37f75cc6d
BLAKE2b-256 504e3c3dcd74c6447ed31c961f54b06c44d9eaee2feec902ed1445b40da67ab0

See more details on using hashes here.

Provenance

The following attestation bundles were made for multiagent_trace_middleware-0.1.0.tar.gz:

Publisher: publish.yml on atom2ueki/multiagent-trace-middleware

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file multiagent_trace_middleware-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for multiagent_trace_middleware-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b61504bcd2069f3e3405b075d0d2e63c893dcec92a2fa96804e426889c673741
MD5 5a72c5c51a8b72e7c8f5d587a18e4e8c
BLAKE2b-256 231e9a1a683c6270481407e904b5b3e62b7da1de9aab1d9846908922e5efa3a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for multiagent_trace_middleware-0.1.0-py3-none-any.whl:

Publisher: publish.yml on atom2ueki/multiagent-trace-middleware

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page