Skip to content

LangChain Integration

Use Cognipeer Console as a LangChain chat model, and capture LangChain runs as Cognipeer tracing sessions. These helpers are exported from the package root.

typescript
import {
  CognipeerLangChainChatModel,
  CognipeerTracingCallbackHandler,
  createCognipeerAgentTracing,
  createCognipeerTracingMiddleware,
} from '@cognipeer/console-sdk';

Requires @langchain/core as a peer dependency.

Chat model

CognipeerLangChainChatModel is a drop-in BaseChatModel that proxies to Console chat completions, so it works anywhere a LangChain chat model does (chains, agents, LCEL).

typescript
import { CognipeerLangChainChatModel } from '@cognipeer/console-sdk';
import { HumanMessage } from '@langchain/core/messages';

const model = new CognipeerLangChainChatModel({
  model: 'gpt-4o-mini',
  client: { apiKey: process.env.COGNIPEER_API_KEY! },
  temperature: 0.2,
});

const res = await model.invoke([new HumanMessage('Summarize LangChain in one line.')]);
console.log(res.content);

Constructor params (CognipeerLangChainChatModelParams):

ParamTypeDescription
modelstringTarget model key on Console (required)
clientConsoleClient | ConsoleClientOptionsAn SDK client or options to build one (required)
temperature, topP, maxTokens, presencePenalty, frequencyPenalty, toolChoiceDefaults applied when a call omits them

Streaming (model.stream(...)) and tool calls are supported through the standard LangChain interfaces.

Tracing callback handler

CognipeerTracingCallbackHandler converts LangChain run events into a Cognipeer tracing session. Attach it via the callbacks array.

typescript
import { CognipeerTracingCallbackHandler } from '@cognipeer/console-sdk';

const tracer = new CognipeerTracingCallbackHandler({
  client: { apiKey: process.env.COGNIPEER_API_KEY! },
  agent: { name: 'support-agent' },
  threadId: 'conversation_42',
  autoFlush: true,
});

await chain.invoke(input, { callbacks: [tracer] });

Options (CognipeerTracingCallbackOptions):

OptionDescription
clientSDK client or options (required)
sessionIdSession id (autogenerated if omitted)
threadIdGroups multiple sessions into one thread
agentAgent descriptor stored with the session
summary, configInitial summary / config blob stored on the session
autoFlushFlush events on chain/tool completion
debug, loggerVerbose logging hooks

Helpers

  • createCognipeerAgentTracing(options) — returns a binding that wires a tracing handler to an agent run with the same options as above.
  • createCognipeerTracingMiddleware(...) — produces middleware for frameworks that take middleware rather than a callbacks array.
typescript
const tracing = createCognipeerAgentTracing({
  client: { apiKey: process.env.COGNIPEER_API_KEY! },
  agent: { name: 'support-agent' },
});

For graph-based agents see the LangGraph integration; for vendor-neutral spans see OpenTelemetry.

Released under the MIT License.