Lesson 3 of 6·10 min read

Setting Up Agent Monitoring

With OpenClaw installed, it's time to connect your AI agents. OpenClaw uses a trace-based model — every agent action is captured as a span within a trace, similar to distributed tracing in microservices.

Connecting Agents

Automatic Instrumentation

For supported frameworks, a single line suffices:

from openclaw import OpenClaw, auto_instrument

oc = OpenClaw(api_key="oc_live_...")
auto_instrument(oc, framework="langchain")  # or "crewai", "autogen", "agentos"

Auto-instrumentation automatically captures:

  • Every LLM call (prompt, response, token count)
  • Tool calls and their results
  • Agent decisions and routing
  • Memory access and context window

Manual Instrumentation

For custom agents or finer control:

with oc.trace("customer-support-agent") as trace:
    trace.set_metadata({"customer_id": "12345", "channel": "chat"})

    with trace.span("intent-classification") as span:
        span.set_input(user_message)
        intent = classify_intent(user_message)
        span.set_output(intent)

    with trace.span("response-generation") as span:
        span.set_model("gpt-4o")
        response = generate_response(intent, context)
        span.set_output(response)
        span.set_tokens(input=245, output=189)

Understanding Trace Structure

An OpenClaw trace maps the complete lifecycle of an agent interaction:

Trace: customer-support-agent (ID: tr_abc123)
├── Span: intent-classification (12ms)
├── Span: knowledge-retrieval (145ms)
│   ├── Span: vector-search (89ms)
│   └── Span: reranking (52ms)
├── Span: response-generation (1,340ms)
│   ├── Span: llm-call (1,200ms)
│   └── Span: guardrail-check (140ms)
└── Span: response-delivery (8ms)

Metadata Tagging

Tags enable precise filtering and grouping:

Tag CategoryExamplesPurpose
Environmentproduction, staging, devEnvironment separation
Teamsupport, sales, opsCost allocation
Agent versionv2.1.0, v2.2.0-betaVersion comparison
Customercustomer_id, tenant_idMulti-tenancy
Compliancerisk_level, pii_detectedGovernance tracking

Real-Time Dashboards

As soon as traces flow in, the live dashboard shows:

  • Agent Activity Feed — Real-time stream of all agent actions
  • Latency Heatmap — Response times over time, grouped by agent
  • Error Rate — Error rate per agent and time window
  • Token Consumption — Real-time usage with trend line
  • Active Agents — Which agents are currently active and their load

Tip: Start with auto-instrumentation and switch to manual spans only where you need deeper insights. Excessive tracing creates noise and increases storage costs.