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 identifierworkflow_name: Human-readable workflow namegroup_id: Optional grouping (e.g., chat session ID)started_at/ended_at: Unix timestampsattributes: Key-value metadata- Spans: Individual operations within the trace
id: Unique span identifierparent_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 timestampsattributes: 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 ingestionGET /v1/traces/{trace_id}: Retrieve trace by IDPOST /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 datausage_input_tokens,usage_output_tokens,usage_total_tokens: Token countstemperature: Sampling temperature
Tool/Function Spans (kind=tool)
function_name: Name of the tool/functionfunction_input: Serialized input argumentsfunction_output: Serialized output
Workflow/Agent Spans (kind=workflow)
agent_tools: Available toolsagent_handoffs: Available handoff targetsagent_output_type: Expected output type
Guardrail Spans (kind=guardrail)
guardrail_triggered: Whether the guardrail was triggered
Handoff Spans (kind=handoff)
handoff_from: Source agenthandoff_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=NTrace envelope prepared; trace_id=... span_count=NTelemetry 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
699423c48852c27ea8cef28641f6938c644fd9a0e8cfa72f57272444d3ffeefb
|
|
| MD5 |
d83c6fe15a66378cae53c5ae53e0951a
|
|
| BLAKE2b-256 |
01f59d762d48365a87177f3614d6cc1bf46c07ca47ad28005b85c01bb4146d47
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ca75cc3086b105ab4d2d9ee269ffae9cd790ab25a43b8ae95f13f60cff8cb8e
|
|
| MD5 |
4bea16376ff6952349199d9da5cf449d
|
|
| BLAKE2b-256 |
0c818f3a12cdfce941c597fb2eb5edd98ff166f53396a8aaed78aa2c676c0b63
|