Skip to main content

Management & Observability SDK for AI Agents

Project description

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.

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

agentbasis-0.1.0.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

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

agentbasis-0.1.0-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file agentbasis-0.1.0.tar.gz.

File metadata

  • Download URL: agentbasis-0.1.0.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for agentbasis-0.1.0.tar.gz
Algorithm Hash digest
SHA256 85d33c5e8edb4f950e47d27faa126de27bf679378ae420a967633a8a342dc262
MD5 87b8e27de0c10d2acf7d1153e11e795e
BLAKE2b-256 e0c82a371533c1cd7fe34aebb4516cc7292f77621b70f18f83054b68b7e31323

See more details on using hashes here.

File details

Details for the file agentbasis-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: agentbasis-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for agentbasis-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0707b3340a416f7df1aff75512f9e60b3a21a21e0ef9d9a4b82f23dad91adbf2
MD5 9d183c7fce4005ef9592c3ba8af0e83c
BLAKE2b-256 dc3276544adedafb7ec926d0ae8ba47277357f97fb1006708a26084cb3e00448

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