LangChain integration for Prefactor observability
Project description
prefactor-langchain
LangChain integration for Prefactor observability. This package provides automatic tracing for LangChain agents using LangChain-specific span types.
Installation
pip install prefactor-langchain
Usage
Factory pattern (quickest setup)
from prefactor_langchain import PrefactorMiddleware
middleware = PrefactorMiddleware.from_config(
api_url="https://api.prefactor.ai",
api_token="your-api-token",
agent_id="my-agent",
agent_name="My Agent", # optional
)
# Use with LangChain's create_agent()
# Your agent will automatically create spans for:
# - Agent execution (langchain:agent)
# - LLM calls (langchain:llm)
# - Tool executions (langchain:tool)
result = agent.invoke({"messages": [...]})
# Middleware owns both client and instance; close when done
await middleware.close()
Pre-configured client
Pass a client you created yourself when you need full control over its configuration or when you want to share a client across multiple middlewares.
from prefactor_core import PrefactorCoreClient, PrefactorCoreConfig
from prefactor_http.config import HttpClientConfig
from prefactor_langchain import PrefactorMiddleware
http_config = HttpClientConfig(api_url="https://api.prefactor.ai", api_token="your-api-token")
config = PrefactorCoreConfig(http_config=http_config)
client = PrefactorCoreClient(config)
await client.initialize()
middleware = PrefactorMiddleware(
client=client,
agent_id="my-agent",
agent_name="My Agent",
)
result = agent.invoke({"messages": [...]})
# You own the client; close both separately
await middleware.close() # closes the agent instance only
await client.close()
Pre-configured instance (spans outside the agent)
Pass an AgentInstanceHandle you created yourself when you also need to
instrument code that lives outside the LangChain agent — for example,
pre-processing steps, post-processing, or any custom business logic that
should appear as siblings of the agent spans in the same trace.
from prefactor_core import PrefactorCoreClient, PrefactorCoreConfig
from prefactor_http.config import HttpClientConfig
from prefactor_langchain import PrefactorMiddleware
http_config = HttpClientConfig(api_url="https://api.prefactor.ai", api_token="your-api-token")
config = PrefactorCoreConfig(http_config=http_config)
client = PrefactorCoreClient(config)
await client.initialize()
instance = await client.create_agent_instance(agent_id="my-agent")
await instance.start()
# Share the instance with the middleware
middleware = PrefactorMiddleware(instance=instance)
# Instrument your own code using the same instance
async with instance.span("custom:preprocessing") as ctx:
ctx.set_payload({"step": "preprocess", "status": "ok"})
# Run your agent — the middleware traces it automatically under the same instance
result = agent.invoke({"messages": [...]})
async with instance.span("custom:postprocessing") as ctx:
ctx.set_payload({"step": "postprocess", "result": str(result)})
# You own the instance and client; clean them up yourself
await instance.finish()
await client.close()
Span Types
This package creates LangChain-specific spans with the langchain:* namespace:
langchain:agent- Agent executions and chain runslangchain:llm- LLM calls with model metadata (name, provider, token usage)langchain:tool- Tool executions including retrievers
Each span payload includes:
- Timing information (start_time, end_time)
- Inputs and outputs
- Error information with stack traces
- LangChain-specific metadata
Trace correlation (span_id, parent_span_id, trace_id) is handled automatically by the prefactor-core client.
Features
- Automatic LLM call tracing - Captures model name, provider, token usage, temperature
- Tool execution tracing - Records tool name, arguments, execution time
- Agent/chain tracing - Tracks agent lifecycle and message history
- Token usage capture - Automatically extracts prompt/completion/total tokens
- Error tracking - Captures error type, message, and stack traces
- Automatic parent-child relationships - Uses SpanContextStack for hierarchy
- Bring your own instance - Share a single
AgentInstanceHandlebetween the middleware and your own instrumentation
Architecture
This package follows the LangChain Adapter Redesign principles:
- Package Isolation: LangChain-specific span types and schemas live in this package
- Opaque Payloads: Span data is sent as payload to prefactor-core
- Type Namespacing: Uses
langchain:agent,langchain:llm,langchain:toolprefixes - Uses prefactor-core: All span/instance management via the prefactor-core client
The middleware:
- Accepts a
PrefactorCoreClient, or a pre-createdAgentInstanceHandle, or creates its own client viafrom_config() - Registers or borrows an agent instance
- Creates spans with LangChain-specific payloads
- Leverages
SpanContextStackfor automatic parent detection
Development
Run tests:
pytest tests/
License
MIT
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file prefactor_langchain-0.2.2.tar.gz.
File metadata
- Download URL: prefactor_langchain-0.2.2.tar.gz
- Upload date:
- Size: 20.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4dd574d5a5d34c6c25d668955807dfcb3bcb2320114e50bf038d03eed11aee1
|
|
| MD5 |
edeb7b8d705c22b66b701289ff3f6462
|
|
| BLAKE2b-256 |
decc71f1ef727003bb3ede1a5b3ea2ab4b97f0fb8e9f7dcc696dace568cb2ae0
|
File details
Details for the file prefactor_langchain-0.2.2-py3-none-any.whl.
File metadata
- Download URL: prefactor_langchain-0.2.2-py3-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"25.10","id":"questing","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb3e0368c66f81b8adba89ac01ed46aff292ec614ca625d4a5e46f5ba6b12d6c
|
|
| MD5 |
2ae37e4a15ae8c643359a4539282e42b
|
|
| BLAKE2b-256 |
8f08d57b01e96ec80b9f784ffdbd6d2e36a869de53cbc0574344fdd9ebff0844
|