agentbasis
Core module functions imported directly fromagentbasis.
Initialization
| Function | Description |
|---|---|
init(api_key: str = None, agent_id: str = None) -> AgentBasis | Initialize the SDK. Reads from AGENTBASIS_API_KEY and AGENTBASIS_AGENT_ID env vars if not provided. |
flush(timeout_millis: int = 30000) -> bool | Force flush all pending telemetry data. Returns True if successful. |
shutdown() -> None | Shut down the SDK and flush pending data. Auto-called on exit. |
Context Management
| Function | Description |
|---|---|
set_user(user_id: str | None) -> None | Set the current user ID globally. |
set_session(session_id: str | None) -> None | Set the current session ID globally. |
set_conversation(conversation_id: str | None) -> None | Set the current conversation ID globally. |
set_metadata(metadata: dict | None) -> None | Set custom metadata globally (e.g., {"plan": "pro"}). |
get_user() -> str | None | Get the current user ID. |
get_session() -> str | None | Get the current session ID. |
get_conversation() -> str | None | Get the current conversation ID. |
get_metadata() -> dict | None | Get the current metadata. |
Context Managers & Decorators
| Function | Description |
|---|---|
context(user_id, session_id, conversation_id, metadata) | Context manager for scoped context. |
with_context(user_id, session_id, conversation_id, metadata) | Decorator for setting context on a function. |
trace | Decorator to track function execution as a span. Works with sync and async. |
agentbasis.llms.openai
| Function | Description |
|---|---|
instrument() -> None | Auto-instrument OpenAI SDK. Traces both OpenAI and AsyncOpenAI clients. |
agentbasis.llms.anthropic
| Function | Description |
|---|---|
instrument() -> None | Auto-instrument Anthropic SDK. Traces both Anthropic and AsyncAnthropic clients. |
agentbasis.llms.gemini
| Function | Description |
|---|---|
instrument() -> None | Auto-instrument Google Gemini SDK. Traces GenerativeModel.generate_content() sync and async. |
agentbasis.frameworks.langchain
| Function | Description |
|---|---|
get_callback_handler() -> AgentBasisCallbackHandler | Get a new callback handler instance. |
instrument() -> AgentBasisCallbackHandler | Get the global singleton callback handler. |
get_callback_config() -> dict | Get a RunnableConfig dict with callbacks pre-configured. |
AgentBasisCallbackHandler
Callback handler that traces:- LLM calls (
on_llm_start/end/error) - Chain execution (
on_chain_start/end/error) - Tool invocations (
on_tool_start/end/error) - Retriever operations (
on_retriever_start/end/error)
agentbasis.frameworks.pydanticai
| Function | Description |
|---|---|
instrument(include_content: bool = True, include_binary_content: bool = False) -> None | Enable global instrumentation for all Pydantic AI agents. |
get_instrumentation_settings(include_content, include_binary_content) -> InstrumentationSettings | Get settings for per-agent instrumentation. |
get_metadata_callback() -> Callable | Get a callback that injects AgentBasis context into traces. |
create_traced_agent(model: str, include_content: bool = True, **kwargs) -> Agent | Create a Pydantic AI agent pre-configured with tracing. |
Parameters
| Parameter | Description |
|---|---|
include_content | Whether to log prompts and completions. Set False for privacy. Default: True |
include_binary_content | Whether to include binary content like images. Default: False |

