Class: PrefactorMiddleware
Prefactor TypeScript SDK / @prefactor/packages/langchain / PrefactorMiddleware
Class: PrefactorMiddleware
Section titled “Class: PrefactorMiddleware”Defined in: packages/langchain/src/middleware.ts:40
Prefactor middleware for LangChain.js agents.
This middleware automatically traces LLM calls, tool executions, and agent workflows. It integrates with LangChain.js middleware API to provide transparent instrumentation.
Features:
- Automatic parent-child span relationships via context propagation
- Token usage extraction for LLM calls
- Error tracking and debugging
- Zero-overhead instrumentation (graceful failure)
Example
Section titled “Example”import { init } from '@prefactor/langchain';import { createReactAgent } from '@langchain/langgraph/prebuilt';
const middleware = init();const agent = createReactAgent({ llm: model, tools: [myTool], middleware: [middleware],});Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new PrefactorMiddleware(
tracer,agentManager,agentInfo?):PrefactorMiddleware
Defined in: packages/langchain/src/middleware.ts:43
Parameters
Section titled “Parameters”tracer
Section titled “tracer”Tracer
agentManager
Section titled “agentManager”AgentInstanceManager
agentInfo?
Section titled “agentInfo?”AgentInstanceOptions
Returns
Section titled “Returns”PrefactorMiddleware
Methods
Section titled “Methods”beforeAgent()
Section titled “beforeAgent()”beforeAgent(
_state):Promise<void>
Defined in: packages/langchain/src/middleware.ts:55
Called before agent execution starts
Parameters
Section titled “Parameters”_state
Section titled “_state”any
Returns
Section titled “Returns”Promise<void>
afterAgent()
Section titled “afterAgent()”afterAgent(
state):Promise<void>
Defined in: packages/langchain/src/middleware.ts:65
Called after agent execution completes
Parameters
Section titled “Parameters”any
Agent state containing messages
Returns
Section titled “Returns”Promise<void>
shutdown()
Section titled “shutdown()”shutdown():
void
Defined in: packages/langchain/src/middleware.ts:70
Returns
Section titled “Returns”void
wrapModelCall()
Section titled “wrapModelCall()”wrapModelCall<
T>(request,handler):Promise<T>
Defined in: packages/langchain/src/middleware.ts:82
Wrap a model call to trace LLM invocations
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”request
Section titled “request”any
Model invocation request
handler
Section titled “handler”(req) => Promise<T>
The actual model call function
Returns
Section titled “Returns”Promise<T>
Promise resolving to the model response
wrapToolCall()
Section titled “wrapToolCall()”wrapToolCall<
T>(request,handler):Promise<T>
Defined in: packages/langchain/src/middleware.ts:117
Wrap a tool call to trace tool executions
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”request
Section titled “request”any
Tool invocation request
handler
Section titled “handler”(req) => Promise<T>
The actual tool call function
Returns
Section titled “Returns”Promise<T>
Promise resolving to the tool response