Skip to main content

AgentBasis Python SDK

Management & Observability SDK for AI Agents in production

The AgentBasis Python SDK provides a simple, lightweight way to track the performance, traces, sessions, and behavior of AI agents. It sends data using the OpenTelemetry (OTel) standard, making it compatible with AgentBasis and other observability backends.

This is the foundation SDK that enables deep observability for coded agents built with:

  • Pure Python
  • LLM Providers:
    • OpenAI
    • Anthropic
    • Gemini
  • Frameworks
    • LangChain
    • Pydantic AI

Installation

pip install agentbasis

Quick Start

1. Initialize the SDK

Start by initializing the SDK with your API key and Agent ID. This usually goes at the top of your main application file.

import agentbasis

# Initialize with your API Key and Agent ID
agentbasis.init(
    api_key="your-api-key-here", 
    agent_id="your-agent-id-here"
)

2. Manual Tracking (Decorators)

Use the @trace decorator to automatically track any function.

from agentbasis import trace

@trace
def chat_with_user(query):
    # Your agent logic here
    return "Response to: " + query

# When you call this, data is automatically sent to AgentBasis
chat_with_user("Hello world")

3. OpenAI Integration

Automatically track all your OpenAI calls (models, tokens, prompts) with one line of code.

from agentbasis.llms.openai import instrument

# Enable OpenAI instrumentation
instrument()

# Now just use the OpenAI client as normal
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello"}]
)

4. Anthropic Integration

Automatically track all your Anthropic Claude calls.

from agentbasis.llms.anthropic import instrument

# Enable Anthropic instrumentation
instrument()

# Now just use the Anthropic client as normal
from anthropic import Anthropic
client = Anthropic()
response = client.messages.create(
    model="claude-3-opus-20240229",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}]
)

5. LangChain Integration

Track chains, tools, retrievers, and LLM calls in LangChain with full parent-child span relationships.

from agentbasis.frameworks.langchain import get_callback_handler

# Create a callback handler
handler = get_callback_handler()

# Pass it to your LangChain calls
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4")
response = llm.invoke("Hello world", config={"callbacks": [handler]})

For chains and agents, pass the callback handler in the config:

from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from agentbasis.frameworks.langchain import get_callback_config

# Use get_callback_config() for convenience
chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template("{query}"))
result = chain.invoke({"query": "What is AI?"}, config=get_callback_config())

6. Pydantic AI Integration

Track Pydantic AI agents with built-in OpenTelemetry support.

from agentbasis.frameworks.pydanticai import instrument

# Enable global instrumentation for all Pydantic AI agents
instrument()

# Your agents are now automatically traced
from pydantic_ai import Agent
agent = Agent("openai:gpt-4")
result = agent.run_sync("Hello!")

For per-agent control with user context:

from agentbasis.frameworks.pydanticai import create_traced_agent

# Create an agent pre-configured with tracing and context
agent = create_traced_agent(
    "openai:gpt-4",
    system_prompt="You are a helpful assistant."
)

# Set user context - it will be included in traces
agentbasis.set_user("user-123")
result = agent.run_sync("Hello!")

7. Track Users & Sessions (Optional)

Associate traces with specific users and sessions to debug issues and see per-user analytics.

# Set the current user (from your auth system)
agentbasis.set_user(current_user.id)

# Optionally set session and conversation IDs
agentbasis.set_session("session-abc")
agentbasis.set_conversation("conv-123")

# All subsequent LLM calls will be tagged with this context
response = client.chat.completions.create(...)

Or use the context manager for scoped context:

from agentbasis import context

with context(user_id="user-123", session_id="session-abc"):
    # All traces in this block include the context
    response = client.chat.completions.create(...)

Core Concepts

  • OpenTelemetry: We use OTel under the hood for maximum compatibility.
  • Spans: Every action (function call, LLM request) is recorded as a Span.
  • Transport: Data is batched and sent asynchronously to the AgentBasis backend.

Documentation

For full documentation, visit docs.agentbasis.co.

Contributing

See CONTRIBUTING.md for development setup and guidelines.

License

MIT License - see LICENSE for details.

Release files for agentbasis 0.1.0

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

Source distribution (sdist)

Source distribution for agentbasis 0.1.0
File Size Uploaded
agentbasis-0.1.0.tar.gz 23.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for agentbasis 0.1.0
File Interpreter ABI Platform
agentbasis-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 48.6 kB

Release files / agentbasis-0.1.0.tar.gz

Download URL agentbasis-0.1.0.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
85d33c5e8edb4f950e47d27faa126de27bf679378ae420a967633a8a342dc262
BLAKE2b-256 checksum
How to use checksums
e0c82a371533c1cd7fe34aebb4516cc7292f77621b70f18f83054b68b7e31323
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Release files / agentbasis-0.1.0-py3-none-any.whl

Download URL agentbasis-0.1.0-py3-none-any.whl
Size 25.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0707b3340a416f7df1aff75512f9e60b3a21a21e0ef9d9a4b82f23dad91adbf2
BLAKE2b-256 checksum
How to use checksums
dc3276544adedafb7ec926d0ae8ba47277357f97fb1006708a26084cb3e00448
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.1.0 This release

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