LangChain API Reference (TypeScript)
Method signatures, option types and extended log fields for the LangChain and LangGraph callback path. For a guide-level introduction, see LangChain.
langchain()
Returns a LangChain BaseCallbackHandler that you pass into callbacks.
Async because LangChain is imported on demand. Create one callback per request
so each request gets its own trace.
Signature
langchain(options?: LangChainCallbackOptions): Promise<BaseCallbackHandler>Parameters
| Name | Type | Default | Description |
|---|---|---|---|
options.role | string | 'unspecified' | Overrides the role set on AiLogger.init() for this callback. |
options.traceId | string | auto | Custom trace id. Defaults to the root run’s runId. |
options.onError | (err: Error, context: CallbackErrorContext) => void | Called when an internal handler error occurs. If omitted, the error and its entry are dropped silently. |
Returns
Promise<BaseCallbackHandler>.
Example
import { AiLogger } from '@sarek/ai-accountability'
const sarek = await AiLogger.init({ name: 'loan-desk' })
const callback = await sarek.langchain({
role: 'credit-assessor',
traceId: 'request-2026-06-16-4821',
onError: (err, context) => {
appLogger.error('sarek callback error', {
handler: context.handler,
runId: context.runId,
err
})
}
})langgraph()
Alias for langchain(). Use it for LangGraph apps; the callback behaves
identically.
Signature
langgraph(options?: LangChainCallbackOptions): Promise<BaseCallbackHandler>Event types
The callback logs five event types. The discriminator is eventType:
| Event | Start handler | Input hash | Output hash |
|---|---|---|---|
llm | chat model or legacy LLM start | the prompt messages or strings | the generated text |
tool | tool start | the tool input | the tool output |
retriever | retriever start | the query | document metadata only, never content |
chain | chain start | the chain inputs | the chain outputs |
error | LLM, tool or chain error | reused from the matching start | empty |
Extended LogEntry fields
On top of the standard LogEntry
fields, the callback path populates these:
| Field | Source | Notes |
|---|---|---|
eventType | the handler that fired | llm, tool, retriever, chain or error |
outcome | the handler that fired | success, or error on a failed call |
traceId | the root run’s runId | equals LangSmith’s trace_id; ties one request together |
runId | the callback’s run id for this event | unique per event |
parentRunId | the callback’s parent run id | null on the root; reconstructs the call tree |
chainName | the serialized chain id | chain events |
toolName | the tool name | tool events |
toolCallId | the model’s tool-call id | links a tool event to the call that requested it |
errorClass | the exception class name | error events |
errorHash | SHA3-256 of the error message | the raw message is never stored |
modelParams | configured from the invocation params, actual from the result | llm events only; see below |
thread_id | metadata.thread_id | LangGraph; null for plain LangChain |
langgraph_node | metadata.langgraph_node | LangGraph; null otherwise |
langgraph_step | metadata.langgraph_step | LangGraph; null otherwise |
checkpoint_ns | metadata.checkpoint_ns | LangGraph; null otherwise |
tags, metadata | passed through from the callback | carry deployer context, end-user id, jurisdiction |
modelParams: configured against actual
On llm events, modelParams separates what you asked for from what the
provider reported:
configuredholdstemperature,maxTokensandtopP, read from the invocation parameters LangChain builds for the call. These are the same values LangSmith logs.actualholds the provider’s own output block, framed as what the provider actually reported. Many providers leave most of it empty, which is expected.
Retriever output is metadata only
For retriever events the output hash is computed over the id and source
of each retrieved document, never the document content. The page content never
leaves the SDK. If you need the content for retention, keep it yourself.
Fields that differ on this path
A few standard LogEntry fields do not carry their usual meaning when the entry
comes from a callback:
| Field | On the callback path |
|---|---|
predictionId | empty; LangChain does not surface a provider response id here |
citations | not populated; this is a Perplexity-only field on the direct path |
statusCode | always 200. Use outcome for success or failure, not the status code |
environment | process.env.NODE_ENV |