Skip to content

Getting started with the Python SDK

The Python SDK is split into small packages. Start with the package that matches how you want to integrate Prefactor.

PackageUse it whenReference
prefactor-coreCore client for agent instances, spans, schema registration, and wrapper authors.Reference
prefactor-httpLow-level async client for direct access to the Prefactor API.Reference
prefactor-langchainLangChain integration package with middleware and LangChain-specific span types.Reference
prefactor-livekitLiveKit integration package for tracing session and voice-agent events.Reference

Install the package you need:

Terminal window
pip install prefactor-langchain

Or install a different package:

Terminal window
pip install prefactor-core
pip install prefactor-http
pip install prefactor-livekit

Most applications start with an integration package. This example uses the LangChain middleware.

agent_id is the Prefactor agent ID (a ULID from prefactor agents create or the Agent page). With a deployment-scoped token you can omit it; with an account-scoped token you must supply it.

import os
from prefactor_langchain import PrefactorMiddleware
middleware = PrefactorMiddleware.from_config(
api_url="https://app.prefactorai.com",
api_token=os.environ["PREFACTOR_API_TOKEN"], # never hardcode tokens
agent_id="01exampleagentid00000000000000000", # Prefactor agent id (ULID)
agent_name="My Agent",
)

If you want to build your own wrapper or instrument code directly, start with prefactor-core:

import os
from prefactor_core import PrefactorCoreClient, PrefactorCoreConfig
from prefactor_http import HttpClientConfig
config = PrefactorCoreConfig(
http_config=HttpClientConfig(
api_url="https://app.prefactorai.com",
api_token=os.environ["PREFACTOR_API_TOKEN"],
)
)
client = PrefactorCoreClient(config)
await client.initialize()