Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿฉบ Telemetry-MCP-Okahu

A Self-Healing AI Agent That Fixes Its Own Bugs โ€” Using Only Production Traces

No guessing. No print-debugging. No local logs. Just telemetry.

Python OpenAI Monocle Okahu FastAPI License

๐ŸŒ Live Project Website โ†’


๐Ÿ’ก The Big Idea

Most AI-coding demos show an agent building an app. This one shows something far more interesting: an agent debugging a broken production service the way a senior SRE would โ€” by reading distributed traces from an observability platform.

The repo ships with a deliberately broken Text-to-SQL API (analyst.py). Every LLM call it makes is auto-instrumented by Monocle and exported to Okahu Cloud. The self-healing agent (@analyst_v3) is then pointed at the failing test suite with one hard rule:

You may not guess. Every fix must be justified by evidence from an Okahu Cloud trace, fetched through the hosted Okahu MCP.

The agent runs the tests, watches them fail, pulls the traces, reads the actual request/response spans, and fixes the code โ€” one evidence-backed bug at a time โ€” until everything is green.

๐Ÿ› The Three Planted Bugs

# Bug Symptom What the Trace Reveals
1 Invalid model name โ€” model="gpt-5.4-typo" Every call throws openai.NotFoundError The span's request attributes show the bogus model name sent to the API
2 Wrong response attribute โ€” response.choices[0].text AttributeError after Bug 1 is fixed The span's response payload shows content lives at .message.content
3 Schema mismatch โ€” system prompt teaches customers/products, real DB has users/orders sqlite3.OperationalError: no such table The span's captured prompt + LLM output expose SQL targeting phantom tables

The bugs are layered: fixing one uncovers the next. The agent can't fix them in a single lucky shot โ€” it must iterate: test โ†’ trace โ†’ diagnose โ†’ archive โ†’ fix โ†’ repeat.

๐Ÿ”ฌ Why Trace-Driven Debugging Matters

  1. Trace-driven, not guess-driven. The agent queries the platform (/okahu:get_latest_traces), not local logs or stack traces. This mirrors how real production incidents are diagnosed.
  2. Infrastructure-native. Observability arrives via a hosted MCP server โ€” the agent talks to Okahu Cloud exactly the way it would talk to any other tool.
  3. Auto-instrumented telemetry. Because the app uses the official openai SDK, Monocle captures every LLM span with zero manual instrumentation code.

โš ๏ธ The One Rule of Monocle Instrumentation

Monocle can only auto-instrument supported SDKs:

Works โœ… Does NOT work โŒ
openai Python SDK Raw requests.post() to LLM APIs
google-genai SDK Direct httpx / aiohttp calls
langchain framework Custom API wrappers without SDK instrumentation
llama-index framework

Always call the LLM through the OpenAI SDK directly:

from openai import OpenAI

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[...],
)

If you swap this for a raw HTTP call, no traces reach Okahu Cloud and the entire self-healing loop goes blind.

๐Ÿ—๏ธ Architecture

flowchart LR
    subgraph App["Buggy Text-to-SQL API"]
        A[main.py<br/>FastAPI] --> B[analyst.py<br/>3 planted bugs]
        B --> C[(sales.db<br/>users / orders)]
    end
    B -- "openai SDK call" --> D[GPT-4o]
    B -. "Monocle auto-instrumentation" .-> E[Okahu Cloud]
    subgraph Agent["@analyst_v3 (OpenCode)"]
        F[Run pytest] --> G[Query hosted Okahu MCP]
        G --> H[Diagnose from trace evidence]
        H --> I[Archive + Fix analyst.py]
        I --> F
    end
    G <-- "/okahu:get_latest_traces" --> E
    F -. "exercises" .-> A
Loading

๐Ÿ“ Repository Layout

telemetry-mcp-okahu/
โ”œโ”€โ”€ analyst.py               # ๐Ÿ› The buggy Text-to-SQL analyst (the patient)
โ”œโ”€โ”€ main.py                  # FastAPI wrapper (POST /query, GET /health)
โ”œโ”€โ”€ test_analyst.py          # Test suite that exposes all 3 bugs
โ”œโ”€โ”€ setup_db.py              # Creates + seeds sales.db (users / orders)
โ”œโ”€โ”€ reset_demo.py            # Restores the buggy state before each demo
โ”œโ”€โ”€ boilerplate.py           # โœ… Known-good reference patterns for the agent
โ”œโ”€โ”€ demo/analyst_buggy.py    # Canonical buggy source used by reset_demo.py
โ”œโ”€โ”€ versions/                # Agent archives analyst_vN.py here before each fix
โ”œโ”€โ”€ .opencode/agent/analyst_v3.md   # The self-healing agent definition
โ”œโ”€โ”€ opencode.example.json    # Hosted Okahu MCP config for OpenCode
โ”œโ”€โ”€ .env.example             # Required environment variables
โ””โ”€โ”€ requirements.txt

๐Ÿš€ Step-by-Step Setup

1. Environment Variables

Set up your keys in the telemetry-mcp-okahu/.env file. You will need an OpenAI API Key and an Okahu API Key for telemetry.

cd telemetry-mcp-okahu
echo 'OPENAI_API_KEY="your-openai-key"' > .env
echo 'OPENAI_MODEL="gpt-4o"' >> .env
echo 'OKAHU_API_KEY="your-okahu-key"' >> .env
echo 'MONOCLE_EXPORTER="okahu"' >> .env

2. Install Dependencies

Create a virtual environment and install required packages:

python3 -m venv venv
source venv/bin/activate        # Windows: venv\Scripts\activate
pip install monocle_apptrace monocle_test_tools openai fastapi uvicorn python-dotenv pytest

3. Initialize the Database

Run the setup script to create and seed the sales.db database:

python setup_db.py

4. Configure OpenCode MCP

Update your global OpenCode config (~/.config/opencode/opencode.json) to use the hosted Okahu MCP:

{
  "mcp": {
    "okahu": {
      "type": "remote",
      "url": "https://mcp.okahu.ai/mcp",
      "headers": {
        "x-api-key": "your-okahu-api-key-here"
      },
      "enabled": true
    }
  }
}

Then re-authenticate:

opencode mcp logout okahu
opencode mcp auth okahu

If prompted to re-authenticate, select "Yes".

๐ŸŽฌ Usage

Reset the Demo (Run Before Each Test)

Always reset to the buggy state before starting a new demo:

python reset_demo.py

This restores analyst.py with all 3 bugs, clears versions/, and rebuilds sales.db.

Run the Self-Healing Agent

Open your OpenCode terminal in the telemetry-mcp-okahu/ directory and run:

"@analyst_v3 Fix the buggy Text-to-SQL API:

The analyst.py, test_analyst.py, and main.py files already exist but have bugs.

1. Run Tests: Execute `pytest test_analyst.py -v` to see failures.
2. Analyze Traces: Wait 5s, then query Okahu MCP (/okahu:get_latest_traces)
   with workflow_name='text_to_sql_analyst_v3'.
3. Fix Loop:
   - Archive current analyst.py to versions/analyst_vN.py
   - Fix the bug based on trace analysis (check boilerplate.py for correct patterns)
   - Record the trace ID used to diagnose each fix
   - Run tests again
   - Repeat until all tests pass
4. Final Report: Output a summary table of all issues fixed with their
   associated trace IDs.

Rules: No debug files. Debug only via Okahu MCP traces. Always call the MCP
tool to get the logs from traces, do not use the local logs in the terminal"

What a Successful Run Looks Like

โŒ 4 failed, 1 passed   โ†’ trace shows model 'gpt-5.4-typo' โ†’ fix model name
โŒ 4 failed, 1 passed   โ†’ trace shows response shape       โ†’ fix .message.content
โŒ 2 failed, 3 passed   โ†’ trace shows SQL on 'customers'   โ†’ fix schema prompt
โœ… 5 passed             โ†’ final report with trace IDs
Bug Root Cause (from trace) Fix Trace ID
Invalid model Request span: model=gpt-5.4-typo โ†’ 404 model="gpt-4o" trace_abc...
Response attr Response span: content at message.content .choices[0].message.content trace_def...
Schema mismatch Output span: SELECT ... FROM customers Prompt now teaches users/orders trace_ghi...

Try the API Directly

uvicorn main:app --reload --port 8000
curl -X POST http://localhost:8000/query \
     -H "Content-Type: application/json" \
     -d '{"question": "How many users are there?"}'

๐Ÿงช The Test Suite

Test Exposes
test_database_has_real_schema Sanity: sales.db really has users + orders
test_generate_sql_returns_select Bug 1 (invalid model) โ†’ Bug 2 (wrong attribute)
test_sql_targets_real_tables Bug 3 (phantom customers/products schema)
test_end_to_end_count_users Full pipeline correctness (8 users)
test_end_to_end_order_revenue Aggregation executes against the real schema

๐Ÿ”ฎ Why This Pattern Is the Future

Today's AI agents fail silently and get "fixed" by prompt tinkering. This POC demonstrates the alternative: agents wired into the same observability stack as the rest of your infrastructure, capable of closing the loop from failure โ†’ telemetry โ†’ root cause โ†’ verified fix โ€” autonomously, and with an audit trail of trace IDs for every change they make.

๐Ÿ”— Links


Built by Tirth Rank ยท If this repo helped you understand trace-driven self-healing agents, โญ star it!

About

Self-healing AI agent that fixes a buggy Text-to-SQL API using only Okahu Cloud traces via the hosted Okahu MCP (Monocle + GPT-4o + FastAPI)

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages