Skip to main content

Python SDK for AI agent telemetry and observability

Project description

Neuraltrust SDK

Python SDK for instrumenting AI agent libraries to send telemetry to the Neuraltrust platform.

Features

  • ๐Ÿ”Œ OpenAI Agents SDK integration via custom trace processor
  • ๐Ÿ“Š Automatic span capture: LLM calls, tool executions, agent runs, handoffs, guardrails
  • ๐Ÿš€ Async batching transport with exponential backoff and retry logic
  • ๐ŸŽฏ Sampling support for high-volume production workloads
  • ๐Ÿ”ง Configurable via environment variables or programmatic config
  • ๐Ÿงช Local test server included for development

Installation

Install the SDK using pip:

pip install neuraltrust

Or with uv:

uv add neuraltrust

Quick Start (OpenAI Agents)

from agents import Agent, Runner
from neuraltrust import register_openai_tracing

# Register the Neuraltrust trace processor
register_openai_tracing()

# Your agent code runs normally
agent = Agent(name="Assistant", instructions="You are helpful")
result = await Runner.run(agent, "Hello!")

Configuration

Environment Variables

# Required
export NEURALTRUST_API_KEY=your-api-key

# Optional
export NEURALTRUST_ENDPOINT=https://telemetry.neuraltrust.ai/v1/traces/batch
export NEURALTRUST_BATCH_SIZE=25
export NEURALTRUST_FLUSH_INTERVAL=2.0
export NEURALTRUST_TIMEOUT=5.0
export NEURALTRUST_MAX_RETRIES=3
export NEURALTRUST_SAMPLING_RATE=1.0

# Custom metadata (any NEURALTRUST_METADATA_* vars)
export NEURALTRUST_METADATA_ENVIRONMENT=production
export NEURALTRUST_METADATA_VERSION=1.2.3

Programmatic Configuration

from neuraltrust import TelemetryConfig, register_openai_tracing

config = TelemetryConfig(
    api_key="your-api-key",
    endpoint="https://telemetry.neuraltrust.ai/v1/traces/batch",
    batch_size=25,
    flush_interval_seconds=2.0,
    sampling_rate=0.1,  # Sample 10% of traces
    metadata={"environment": "staging", "version": "2.0.0"},
)

register_openai_tracing(config=config, replace_default=True)

Replace Default Exporters

By default, the SDK adds Neuraltrust telemetry in addition to OpenAI's default trace exporter. To disable OpenAI's exporter and send traces only to Neuraltrust:

register_openai_tracing(replace_default=True)

Or via environment variable:

export NEURALTRUST_REPLACE_DEFAULT=1

Development & Testing

Local Test Server

Start the included test API server:

uv run uvicorn examples.api.temp_server:app --host 127.0.0.1 --port 8000

Configure your SDK to point to it:

export NEURALTRUST_ENDPOINT=http://127.0.0.1:8000/v1/traces/batch
export NEURALTRUST_API_KEY=dummy
export NEURALTRUST_REPLACE_DEFAULT=1

Run the example:

uv run python examples/openai_agents/basic.py "your message here"

Inspect captured traces:

curl http://127.0.0.1:8000/v1/traces | jq

Reset stored traces:

curl -X DELETE http://127.0.0.1:8000/v1/traces

Trace Data Structure

Each trace envelope contains:

  • Resource metadata: SDK version, language, library name/version
  • Traces: One or more logical workflows
    • trace_id: Unique identifier
    • workflow_name: Human-readable workflow name
    • group_id: Optional grouping (e.g., chat session ID)
    • started_at / ended_at: Unix timestamps
    • attributes: Key-value metadata
    • Spans: Individual operations within the trace
      • id: Unique span identifier
      • parent_id: Parent span (null for root)
      • name: Operation name (e.g., "Echo", "Response", "translate_to_french")
      • kind: Span type (workflow, llm, tool, guardrail, handoff)
      • status: Execution status (ok, error)
      • started_at / ended_at: Unix timestamps
      • attributes: Span-specific metadata (model, usage tokens, I/O, etc.)
      • events: Time-stamped events within the span

Backend API Specification

See docs/backend/endpoints.md for the complete backend API specification, including:

  • POST /v1/traces/batch: Batch trace ingestion
  • GET /v1/traces/{trace_id}: Retrieve trace by ID
  • POST /v1/traces/heartbeat: Client health check
  • Authentication, error codes, retry logic

Span Attributes

LLM Spans (kind=llm)

  • model: Model identifier (e.g., gpt-4o-mini-2024-07-18)
  • response_model, response_output: Response data
  • usage_input_tokens, usage_output_tokens, usage_total_tokens: Token counts
  • temperature: Sampling temperature

Tool/Function Spans (kind=tool)

  • function_name: Name of the tool/function
  • function_input: Serialized input arguments
  • function_output: Serialized output

Workflow/Agent Spans (kind=workflow)

  • agent_tools: Available tools
  • agent_handoffs: Available handoff targets
  • agent_output_type: Expected output type

Guardrail Spans (kind=guardrail)

  • guardrail_triggered: Whether the guardrail was triggered

Handoff Spans (kind=handoff)

  • handoff_from: Source agent
  • handoff_to: Target agent

Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ OpenAI Agents SDK                           โ”‚
โ”‚   โ”œโ”€ Agent runs                             โ”‚
โ”‚   โ”œโ”€ LLM generations                        โ”‚
โ”‚   โ””โ”€ Tool calls                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
               โ”‚ TracingProcessor interface
               โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ NeuraltrustTraceProcessor                   โ”‚
โ”‚   โ”œโ”€ Buffers spans per trace                โ”‚
โ”‚   โ”œโ”€ Extracts span_data attributes          โ”‚
โ”‚   โ””โ”€ Emits TraceEnvelope on trace end       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
               โ”‚
               โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ TelemetryClient                             โ”‚
โ”‚   โ”œโ”€ Sampling                               โ”‚
โ”‚   โ””โ”€ Async envelope queueing                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
               โ”‚
               โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ TelemetryTransport                          โ”‚
โ”‚   โ”œโ”€ Background flush loop                  โ”‚
โ”‚   โ”œโ”€ Batching (default: 25 items)           โ”‚
โ”‚   โ”œโ”€ Exponential backoff retry              โ”‚
โ”‚   โ””โ”€ HTTP POST with Bearer auth             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
               โ”‚
               โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Neuraltrust Telemetry API                   โ”‚
โ”‚   POST /v1/traces/batch                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Debugging

Enable debug logging to see trace/span lifecycle events:

import logging
logging.basicConfig(level=logging.DEBUG)

Or via environment:

export LOG_LEVEL=DEBUG

Key log messages:

  • Trace started; trace_id=...
  • Span ended; span_id=... stored_spans=N
  • Trace envelope prepared; trace_id=... span_count=N
  • Telemetry batch sent; items=N status=202

Future Roadmap

  • LangChain integration
  • LlamaIndex integration
  • CrewAI integration
  • OpenTelemetry exporter
  • Prometheus metrics
  • Local SQLite storage option

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

neuraltrust-1.5.0.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

neuraltrust-1.5.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file neuraltrust-1.5.0.tar.gz.

File metadata

  • Download URL: neuraltrust-1.5.0.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for neuraltrust-1.5.0.tar.gz
Algorithm Hash digest
SHA256 699423c48852c27ea8cef28641f6938c644fd9a0e8cfa72f57272444d3ffeefb
MD5 d83c6fe15a66378cae53c5ae53e0951a
BLAKE2b-256 01f59d762d48365a87177f3614d6cc1bf46c07ca47ad28005b85c01bb4146d47

See more details on using hashes here.

File details

Details for the file neuraltrust-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: neuraltrust-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0

File hashes

Hashes for neuraltrust-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ca75cc3086b105ab4d2d9ee269ffae9cd790ab25a43b8ae95f13f60cff8cb8e
MD5 4bea16376ff6952349199d9da5cf449d
BLAKE2b-256 0c818f3a12cdfce941c597fb2eb5edd98ff166f53396a8aaed78aa2c676c0b63

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