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.

Async Functions

The @trace decorator works with both sync and async functions:

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

Set context globally, which applies to all subsequent operations until changed:

Reading Context

You can read the current context values:

Scoped Context (Context Manager)

For multi-threaded or async applications, use the context manager to scope data to a specific block:

Scoped Context (Decorator)

Use @with_context to set context for an entire function:

Flush & Shutdown

Flushing Data

The SDK batches and sends data asynchronously. If you need to ensure all data is sent at a specific point, use flush():
Common use cases for flush():
  • Before a critical operation
  • At specific checkpoints in your application
  • In serverless functions before the function terminates

Shutdown

The SDK automatically shuts down and flushes on normal Python exit. If you need to manually shut down:
shutdown() is idempotent - calling it multiple times is safe.

How It Works

  • OpenTelemetry: We use OTel under the hood for maximum compatibility with observability tools.
  • Spans: Every action (function call, LLM request) is recorded as a Span with timing, inputs, and outputs.
  • Transport: Data is batched and sent asynchronously, ensuring minimal impact on your application’s latency.
  • Context Propagation: User, session, and metadata are automatically attached to all spans.