Skip to main content

Kindred Tracer SDK for Python - Auto-instrumentation for AI agents

Project description

kindred-tracer

Kindred Tracer SDK for Python - Auto-instrumentation for AI agents.

This package automatically intercepts HTTP requests from your AI agent, categorizes them as LLM calls or tool executions, and exports logs to the Kindred log-search system.

Installation

pip install kindred-tracer

Or with optional dependencies:

pip install kindred-tracer[all]  # Includes requests support

Usage

Basic Usage

Just call kindred_tracer() once at startup, and all HTTP requests will be automatically intercepted and logged:

from kindred_tracer import kindred_tracer
import openai

# At startup - initialize the tracer
kindred_tracer()

# Your agent code here - no wrapping needed!
# All HTTP requests will be automatically logged
client = openai.OpenAI()
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

Async Usage

Works the same way with async code:

import asyncio
from kindred_tracer import kindred_tracer
from openai import AsyncOpenAI

# Initialize once at startup
kindred_tracer()

async def main():
    # No wrapping needed - all requests are automatically logged
    client = AsyncOpenAI()
    response = await client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Hello!"}]
    )

asyncio.run(main())

Configuration

Set the following environment variables:

  • KINDRED_API_KEY (required) - Your Kindred API key for authentication
  • KINDRED_API_URL (optional) - Base URL for Kindred API, defaults to https://api.usekindred.dev
  • KINDRED_SESSION_ID (optional) - Session identifier. If not set, a UUID will be auto-generated
  • KINDRED_AGENT_ID (optional) - Agent identifier
  • KINDRED_RUN_ID (optional) - Run identifier

You can also pass these values directly to kindred_tracer():

from kindred_tracer import kindred_tracer

# Initialize with explicit values
kindred_tracer(session_id='session-123', agent_id='agent-456', run_id='run-789')

How It Works

  1. Simple Initialization: Call kindred_tracer() once at startup to set up global context and enable interception.

  2. Auto-instrumentation: The tracer automatically patches httpx.Client.request and httpx.AsyncClient.request when initialized. This is critical because OpenAI SDK v1+ uses httpx internally.

  3. Global Context: Uses a global context that applies to all HTTP requests after initialization.

  4. Request Detection:

    • LLM Calls: Detected by hostname (e.g., api.openai.com, api.anthropic.com) → logged as role: "agent"
    • Tool Calls: Any other hostname → logged as role: "tool"
  5. Non-blocking Export: Logs are batched and exported in a background thread to avoid slowing down your agent.

Log Format

Logs are automatically formatted and sent to ${KINDRED_API_URL}/api/logs/ingest with the following structure:

{
    "session_id": str,
    "timestamp": str,  # ISO 8601
    "role": "user" | "agent" | "tool" | "system",
    "content": str,
    "agent_id": str | None,
    "run_id": str | None,
    "meta": {
        "type": "llm_generation" | "tool_execution",
        "request_id": str,
        "host": str,
        "method": str,
        "path": str,
        "request_headers": dict,
        "request_body": str | None,
        "response_status": int | None,
        "response_headers": dict,
        "response_body": str | None,
        "duration_ms": float,
        "tool_calls": list | None,  # Extracted from OpenAI responses
    }
}

Flushing Logs

Before shutting down your application, you can flush any pending logs:

from kindred_tracer import flush

# On shutdown
flush()

Security

The tracer automatically sanitizes sensitive headers before logging:

  • Authorization
  • x-api-key
  • api-key
  • x-auth-token
  • cookie

Supported LLM Providers

The tracer automatically detects requests to:

  • OpenAI (api.openai.com)
  • Anthropic (api.anthropic.com)
  • Google Gemini (generativelanguage.googleapis.com)
  • Cohere (api.cohere.com)
  • Mistral (api.mistral.ai)

Example

Here's a complete example:

from kindred_tracer import kindred_tracer, flush
import openai
import os

# Set your API key
os.environ['KINDRED_API_KEY'] = 'your-api-key-here'

# Initialize the tracer (reads session_id from KINDRED_SESSION_ID env var, or auto-generates)
kindred_tracer()

# Your agent code - all HTTP requests are automatically logged
client = openai.OpenAI()
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Before shutdown, flush any pending logs
flush()

Requirements

  • Python 3.10+
  • wrapt>=1.14.0 (for safe monkey-patching)
  • httpx>=0.24.0 (for HTTP client and patching)

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

kindred_tracer-1.0.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

kindred_tracer-1.0.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file kindred_tracer-1.0.0.tar.gz.

File metadata

  • Download URL: kindred_tracer-1.0.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for kindred_tracer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a124bc8e2ace613010c18844091924a56d4a6881e81143fe33f05013854a503f
MD5 71e2ce7e066a2b25eb2323a5f3230af8
BLAKE2b-256 467359ea640048f9d643f7f11ff4c32f0acd79e0ac2ba13a776782e65a5c5683

See more details on using hashes here.

File details

Details for the file kindred_tracer-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: kindred_tracer-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for kindred_tracer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ce25c3f488f6a88a47b66e1a552b0724aa6d3b9f0299cec56a56ce62feab0dd1
MD5 794210f53648dd9a204daaa40ae9720a
BLAKE2b-256 d40c9def7c79da9e4355b70f92761ebd34755d72e8563cbb15b5982ef0bbb6d1

See more details on using hashes here.

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