Skip to main content
The AgentBasis SDK provides powerful primitives for manually tracking your application logic and associating data with specific users.

Manual Tracing

Use the @trace decorator to automatically track any function execution as a span. This captures arguments, return values, and execution time.
from agentbasis import trace

@trace
def process_user_input(text):
    # This entire function execution is recorded
    result = complex_logic(text)
    return result

User & Session Context

Context allows you to associate traces with specific users or sessions, enabling you to debug issues per-user or view session-based analytics.

Global Context

You can set context globally, which will apply to all subsequent operations until changed.
import agentbasis

# Set the current user (e.g., from your auth system)
agentbasis.set_user("user-123")

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

# All subsequent calls (LLM or manual) will be tagged with this context

Scoped Context

For multi-threaded or async applications where you handle multiple requests simultaneously, use the context manager to scope data to a specific block.
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(...)

How It Works

  • 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, ensuring minimal impact on your application’s latency.