Skip to main content

MonkAI Trace - Python SDK

Official Python client for MonkAI - Monitor, analyze, and optimize your AI agents.

PyPI version Python 3.8+ License: MIT

📖 REST API reference: trace-docs.monkai.com.br — interactive ReDoc with curl/Node/Python samples per endpoint (also at bemonkai.github.io/monkai-trace) · Migration guide · API changelog · OpenAPI spec · Postman collection

Features

  • Upload conversation records with full token segmentation
  • Track 4 token types: input, output, process, memory
  • Async support via AsyncMonkAIClient (aiohttp-based)
  • Retry with exponential backoff on transient failures
  • Batch processing with automatic chunking
  • Upload from JSON files (supports your existing data)
  • Session management with automatic cleanup and configurable timeouts
  • Data export - Query records/logs with filters, export to JSON or CSV
  • Structured logging via Python logging module
  • HTTP REST API - Language-agnostic tracing for any runtime
  • Framework Integrations:
    • MonkAI Agent - Native framework with automatic tracking
    • LangChain - Full callback handler support (v0.2+)
    • OpenAI Agents - RunHooks integration
    • Python Logging - Standard logging handler with custom_object metadata
  • Coding Assistant Integrations:
    • Claude Code - Parse CLI session logs from ~/.claude/
    • Cline - Parse VS Code extension task history (also Cursor, Windsurf)
    • OpenClaw - Parse personal AI assistant session transcripts
    • GitHub Copilot - Chat history, org usage API, CSV imports

Installation

pip install monkai-trace

For framework integrations:

# MonkAI Agent (Native Framework)
pip install monkai-trace monkai-agent

# LangChain
pip install monkai-trace langchain

# OpenAI Agents
pip install monkai-trace openai-agents-python

Quick Start

Basic Usage

from monkai_trace import MonkAIClient

client = MonkAIClient(tracer_token="tk_your_token")

client.upload_record(
    namespace="customer-support",
    agent="support-bot",
    messages=[
        {"role": "user", "content": "Hello"},
        {"role": "assistant", "content": "Hi! How can I help?"}
    ],
    input_tokens=5,
    output_tokens=10,
    process_tokens=100,
    memory_tokens=20
)

Async Client

from monkai_trace import AsyncMonkAIClient

async def main():
    client = AsyncMonkAIClient(tracer_token="tk_your_token")
    await client.upload_record(
        namespace="my-agent",
        agent="assistant",
        messages=[{"role": "user", "content": "Hello"}],
        input_tokens=5,
        output_tokens=10
    )
    await client.close()

OpenAI Agents Integration

from agents import Agent, WebSearchTool
from monkai_trace.integrations.openai_agents import MonkAIRunHooks

hooks = MonkAIRunHooks(
    tracer_token="tk_your_token",
    namespace="my-agent",
    batch_size=1
)

agent = Agent(
    name="Assistant",
    instructions="You are helpful",
    tools=[WebSearchTool()]
)

hooks.set_user_id("user_abc123")
hooks.set_user_name("João Silva")
hooks.set_user_channel("whatsapp")

result = await MonkAIRunHooks.run_with_tracking(agent, "Hello!", hooks)

LangChain Integration

from langchain.agents import initialize_agent, load_tools
from langchain.llms import OpenAI
from monkai_trace.integrations.langchain import MonkAICallbackHandler

handler = MonkAICallbackHandler(
    tracer_token="tk_your_token",
    namespace="my-agents"
)

llm = OpenAI(temperature=0)
tools = load_tools(["serpapi"], llm=llm)
agent = initialize_agent(tools, llm, callbacks=[handler])
agent.run("What is the weather in Tokyo?")

MonkAI Agent Framework

from monkai_agent import Agent
from monkai_trace.integrations.monkai_agent import MonkAIAgentHooks

hooks = MonkAIAgentHooks(
    tracer_token="tk_your_token",
    namespace="my-namespace"
)

agent = Agent(
    name="Support Bot",
    instructions="You are a helpful assistant",
    hooks=hooks
)

result = agent.run("Help me with my order")

Claude Code Integration

from monkai_trace import ClaudeCodeTracer

tracer = ClaudeCodeTracer(tracer_token="tk_your_token", namespace="dev-productivity")

# Upload all Claude Code sessions
tracer.upload_all_projects()

# Or a specific project
tracer.upload_project("~/.claude/projects/-Users-me-myproject/")

Auto-trace every Claude Code session

Register a one-time hook and every session is uploaded automatically when it ends — no manual command, visible in the MonkAI Hub Monitoring view:

pip install monkai-trace

# Export your tracer token (from MyAgents in the Hub) in your shell profile:
export MONKAI_TRACE_TOKEN="tk_your_token"
export MONKAI_TRACE_NAMESPACE="claude-code"   # optional, this is the default

# Register the SessionEnd hook in ~/.claude/settings.json (idempotent):
monkai-trace install-hook

The hook runs monkai-trace claude-hook, which reads the session transcript path from Claude Code and uploads it. It never raises, so a trace failure can never break your Claude Code session. Remove it anytime with monkai-trace uninstall-hook.

The transcript is uploaded once, when the session ends (SessionEnd). If you resume a session and end it again, it is re-uploaded in full.

🔒 Protect your token file. When the token is resolved from a file (MONKAI_TRACE_TOKEN_FILE, default ~/.monkai_trace_token) instead of the MONKAI_TRACE_TOKEN env var, restrict it so other users on the machine can't read your tracer token — it authenticates all of your trace traffic:

chmod 600 ~/.monkai_trace_token

Cline Integration

from monkai_trace import ClineTracer

tracer = ClineTracer(tracer_token="tk_your_token", namespace="dev-productivity")

# Auto-detects VS Code, Cursor, or Windsurf
tracer.upload_all_tasks()

OpenClaw Integration

from monkai_trace import OpenClawTracer

tracer = OpenClawTracer(tracer_token="tk_your_token", namespace="dev-productivity")

# Upload all sessions from ~/.openclaw/
tracer.upload_all_sessions()

GitHub Copilot Integration

from monkai_trace import CopilotTracer

tracer = CopilotTracer(tracer_token="tk_your_token", namespace="dev-productivity")

# Local chat history
tracer.upload_chat_history()

# Org usage API (Business/Enterprise)
tracer.upload_org_usage(github_token="ghp_xxx", org="MyOrg")

# CSV import
tracer.upload_from_csv("copilot_export.csv")

Upload from JSON Files

client.upload_records_from_json("records.json")
client.upload_logs_from_json("logs.json", namespace="my-agent")

Query & Export Data

result = client.query_records(
    namespace="customer-support",
    agent="Support Bot",
    start_date="2025-01-01",
    limit=50
)

client.export_records(
    namespace="customer-support",
    output_file="conversations.json"
)

client.export_logs(
    namespace="my-agent",
    level="error",
    format="csv",
    output_file="errors.csv"
)

HTTP REST API (Language-Agnostic)

For non-Python runtimes or direct HTTP calls:

import requests

MONKAI_API = "https://lpvbvnqrozlwalnkvrgk.supabase.co/functions/v1/monkai-api"
TOKEN = "tk_your_token"

session = requests.post(
    f"{MONKAI_API}/sessions/create",
    headers={"tracer_token": TOKEN, "Content-Type": "application/json"},
    json={"namespace": "my-agent", "user_id": "user123"}
).json()

requests.post(
    f"{MONKAI_API}/traces/llm",
    headers={"tracer_token": TOKEN, "Content-Type": "application/json"},
    json={
        "session_id": session["session_id"],
        "model": "gpt-4",
        "input": {"messages": [{"role": "user", "content": "Hello"}]},
        "output": {"content": "Hi!", "usage": {"prompt_tokens": 5, "completion_tokens": 3}}
    }
)

See HTTP REST API Guide for complete documentation.

Session Management

MonkAI automatically manages user sessions with configurable timeouts:

  • Default timeout: 2 minutes of inactivity
  • Automatic cleanup: Background thread removes expired sessions
  • Multi-user support: Each user gets isolated sessions
  • Persistent sessions: Optional file-backed session storage with LRU caching
hooks = MonkAIRunHooks(
    tracer_token="tk_your_token",
    namespace="support",
    inactivity_timeout=120
)
hooks.set_user_id("customer-12345")

See Session Management Guide for details.

Token Segmentation

Track 4 token types to understand LLM costs:

Type Description
Input User queries and prompts
Output Agent responses and completions
Process System prompts, instructions, tool definitions
Memory Conversation history and context
client.upload_record(
    namespace="analytics",
    agent="data-agent",
    messages={"role": "user", "content": "Analyze this"},
    input_tokens=15,
    output_tokens=200,
    process_tokens=500,
    memory_tokens=100
)

Examples

See the examples/ directory:

Example Description
openai_agents_example.py OpenAI Agents basic integration
openai_agents_multi_agent.py Multi-agent handoff patterns
monkai_agent_example.py MonkAI Agent framework
langchain_example.py LangChain integration
langchain_conversational.py LangChain with memory
logging_example.py Python logging (scripts)
service_logging_example.py Python logging (long-running services)
session_management_basic.py Automatic session creation
session_management_multi_user.py WhatsApp bot with concurrent users
session_management_custom_timeout.py Custom timeout configuration
http_rest_basic.py HTTP REST API basic usage
http_rest_async.py Async HTTP REST client
http_rest_openai.py OpenAI + HTTP REST tracing
export_data.py Query and export data to JSON/CSV
send_json_files.py Upload from JSON files
claude_code_example.py Parse Claude Code session logs
cline_example.py Parse Cline/OpenClaw task history
openclaw_example.py Parse OpenClaw session transcripts
copilot_example.py Track GitHub Copilot usage

See examples/README.md for the full guide.

Documentation

Development

git clone https://github.com/BeMonkAI/monkai-trace.git
cd monkai-trace

pip install -e ".[dev]"

pytest tests/ -x -q

Requirements

  • Python 3.8+
  • requests >= 2.32.2
  • pydantic >= 2.0.0
  • aiohttp (optional, for AsyncMonkAIClient)
  • monkai-agent (optional, for MonkAI Agent integration)
  • langchain (optional, for LangChain integration)
  • openai-agents-python (optional, for OpenAI Agents integration)

Changelog

v0.7.0

  • Incremental upload (per-session offsets) — ClaudeCodeTracer.upload_session_incremental() uploads only the turns not sent before, persisting an offset per session_id under ~/.monkai_trace/ (override with MONKAI_TRACE_STATE_DIR). The hook now uses it, so a session can fire on every Claude Code Stop (near-realtime) without duplicating, and resumed SessionEnd is safe too.
  • monkai-trace install-hook is robust — registers an absolute, resolvable command (shutil.which / python -m monkai_trace.cli) so the hook runs even when monkai-trace is not on the hook runner's PATH. New --token-file (bakes MONKAI_TRACE_TOKEN_FILE) and --event Stop; warns at install if no token is resolvable yet.
  • Token from file — resolve_token() falls back to MONKAI_TRACE_TOKEN_FILE (default ~/.monkai_trace_token) when MONKAI_TRACE_TOKEN is unset, so the hook does not depend on the shell profile being sourced.
  • monkai-trace watch <dir> / ClaudeCodeTracer.watch() — poll a project directory and upload incrementally, for environments without a hook.

v0.6.1

  • Security: redact PII inside tool_calls — tool-call arguments (file paths, emails, free-text) are now anonymized by the baseline anonymizer. Previously the tool_calls field bypassed redaction entirely.
  • Messages with content: null (tool-only assistant turns, tool messages) no longer emit a spurious "PII may be transmitted unredacted" warning; the warning is reserved for genuinely unexpected content types.

v0.6.0

  • New: monkai-trace CLI with a Claude Code auto-trace hook
    • monkai-trace install-hook registers a SessionEnd hook in ~/.claude/settings.json so every session uploads automatically
    • monkai-trace claude-hook reads the hook payload from stdin and uploads (never raises — a trace failure can't break your session)
    • monkai-trace uninstall-hook, upload-session, upload-project for manual control
    • Config via MONKAI_TRACE_TOKEN / MONKAI_TRACE_NAMESPACE / MONKAI_TRACE_BASE_URL
    • ClaudeCodeTracer and run_hook now exported from monkai_trace.integrations

v0.3.0

  • New: Coding Assistant Integrations
    • ClaudeCodeTracer — Parse Claude Code CLI session logs from ~/.claude/
    • ClineTracer — Parse Cline VS Code extension task history (VS Code, Cursor, Windsurf)
    • OpenClawTracer — Parse OpenClaw personal AI assistant transcripts from ~/.openclaw/
    • CopilotTracer — Track GitHub Copilot via Chat history, org usage API, and CSV imports
  • New: source field on ConversationRecord to identify data origin (claude-code, cline, openclaw, copilot)
  • New: TokenUsage.from_anthropic_usage() — Parse Anthropic API usage dicts with cache token support
  • Added 4 examples and comprehensive documentation guide

v0.2.18

  • Updated README and project URLs
  • Synchronized repository metadata

v0.2.17

  • Security: Patched requests dependency (CVE fix, now >= 2.32.2)
  • Security: Added .env files to .gitignore
  • Security: Replaced bare except: with specific exception handling
  • Reliability: Added retry with exponential backoff on all HTTP requests
  • Reliability: Added CI test gate before PyPI publish
  • Usability: Unified async client (base URL, auth headers, endpoints)
  • Usability: Exported AsyncMonkAIClient from package __init__
  • Usability: Fixed TokenUsage.total_tokens auto-calculation
  • Scalability: Automatic session cleanup via background thread
  • Scalability: Microsecond-precision session IDs to prevent collisions
  • Quality: Migrated all print() calls to logging module

License

MIT License - see LICENSE file.

Support

Contributing

Contributions welcome! Please read our Contributing Guide first.

Metadata

Release files for monkai-trace 0.9.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for monkai-trace 0.9.2
File Size Uploaded
monkai_trace-0.9.2.tar.gz 95.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for monkai-trace 0.9.2
File Interpreter ABI Platform
monkai_trace-0.9.2-py3-none-any.whl Python 3 none any Details

Total release size: 168.8 kB

Release files / monkai_trace-0.9.2.tar.gz

Download URL monkai_trace-0.9.2.tar.gz
Size 95.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1c31d89b050c461d439b3072c32cd3aa770bde16de7b9e57c089a4d170b34b8e
BLAKE2b-256 checksum
How to use checksums
671fc12a5d139844c15fb285f7513358dffa344b2c3931ae6518fb006587f1f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / monkai_trace-0.9.2-py3-none-any.whl

Download URL monkai_trace-0.9.2-py3-none-any.whl
Size 72.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
727035b66992f3fd6db05e51dcfbf8d2f3bc069e858bb737bd62a3669fe296c3
BLAKE2b-256 checksum
How to use checksums
12fea0ae8bf99c24fe78223e22d6778ac64881b98b64cf14169b363c9070fbc3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

0.9.3

2 release files

This release

0.9.2 This release

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.16

2 release files

0.2.15

2 release files

0.2.14

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page