Skip to main content

Plugins

The SDK provides plugins for automatic tracing of popular frameworks.

OpenAI Integration​

Automatically trace OpenAI API calls by wrapping your OpenAI client.

Installation​

pnpm add openai

Basic Usage​

import { MentioraClient } from '@mentiora.ai/sdk';
import { trackOpenAI } from '@mentiora.ai/sdk/openai';
import OpenAI from 'openai';

// Initialize Mentiora client
const mentioraClient = new MentioraClient({
apiKey: process.env.MENTIORA_API_KEY,
});

// Initialize OpenAI client
const openaiClient = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});

// Wrap OpenAI client with tracing
const trackedClient = trackOpenAI(openaiClient, {
mentioraClient,
tags: ['production', 'openai-integration'],
metadata: { environment: 'prod' },
});

// Use trackedClient instead of openaiClient - chat completions are automatically traced
const response = await trackedClient.chat.completions.create({
model: 'gpt-5-mini',
messages: [{ role: 'user', content: 'Hello!' }],
});

For a complete runnable example, see examples/typescript/openai-integration.

The OpenAI plugin traces chat completions (via chat.completions.create); embeddings and image calls are not currently traced.

Multi-turn Conversations​

To group multiple OpenAI calls into a conversation, provide a threadId (TypeScript) or thread_id (Python) when wrapping the client:

import { MentioraClient } from '@mentiora.ai/sdk';
import { trackOpenAI } from '@mentiora.ai/sdk/openai';
import { v7 as uuidv7 } from 'uuid';

// One threadId per conversation (see Thread ID section above)
const threadId = uuidv7();

// Wrap client with threadId - all calls will be grouped
const trackedClient = trackOpenAI(openaiClient, {
mentioraClient,
threadId: threadId, // All traces will use this threadId
tags: ['conversation'],
});

// Turn 1
const response1 = await trackedClient.chat.completions.create({
model: 'gpt-5-mini',
messages: [{ role: 'user', content: 'What is TypeScript?' }],
});

// Turn 2 - same trackedClient = same threadId
const response2 = await trackedClient.chat.completions.create({
model: 'gpt-5-mini',
messages: [
{ role: 'user', content: 'What is TypeScript?' },
{ role: 'assistant', content: response1.choices[0].message.content },
{ role: 'user', content: 'What are its main benefits?' },
],
});

// Both calls are grouped together in the platform by threadId

The plugin automatically captures:

  • All request parameters (model, messages, temperature, max_tokens, tools, response_format, etc.)
  • Multimodal message support (text + image content arrays)
  • Response content and metadata (id, created, system_fingerprint, service_tier)
  • Token usage (prompt, completion, total) for both streaming and non-streaming
  • Refusal handling (when the model refuses a request)
  • Tool/function calls and responses
  • OpenAI response ID (openai_id in trace metadata) for correlation with OpenAI logs
  • Errors and stack traces
  • Duration and timestamps
  • Thread ID for conversation grouping
note

For streaming requests, the plugin automatically injects stream_options: { include_usage: true } to capture token usage. This does not affect your application behavior.

LangChain Integration​

Automatically trace LangChain executions using the callback handler.

Installation​

pnpm add @langchain/core @langchain/openai langchain

Basic Usage​

import { MentioraClient } from '@mentiora.ai/sdk';
import { MentioraTracingLangChain } from '@mentiora.ai/sdk/langchain';
import { ChatOpenAI } from '@langchain/openai';
import { ChatPromptTemplate } from '@langchain/core/prompts';

// Initialize Mentiora client
const mentioraClient = new MentioraClient({
apiKey: process.env.MENTIORA_API_KEY,
});

// Create callback handler
const callback = new MentioraTracingLangChain({
mentioraClient,
tags: ['production', 'langchain-integration'],
metadata: { environment: 'prod' },
});

// Use with LangChain LCEL chains
const llm = new ChatOpenAI({ model: 'gpt-5-mini' });
const prompt = ChatPromptTemplate.fromTemplate('Say hello to {name}');
const chain = prompt.pipe(llm);

// Invoke with callback - all operations are automatically traced
await chain.invoke({ name: 'World' }, { callbacks: [callback] });

// Works with agents, tools, retrievers, and other LangChain components

For a complete runnable example, see examples/typescript/langchain-integration.

Multi-turn Conversations​

To group multiple LangChain operations into a conversation, provide a threadId (TypeScript) or thread_id (Python) when creating the callback:

import { v7 as uuidv7 } from 'uuid';

// One threadId per conversation (see Thread ID section above)
const threadId = uuidv7();

// Create callback with threadId - all traces will be grouped
const callback = new MentioraTracingLangChain({
mentioraClient,
threadId: threadId, // All traces will use this threadId
tags: ['conversation'],
});

// Turn 1
const chain1 = ChatPromptTemplate.fromTemplate(
'Generate a question about {topic}',
).pipe(llm);
const result1 = await chain1.invoke(
{ topic: 'TypeScript' },
{ callbacks: [callback] },
);

// Turn 2 - same callback = same threadId
const chain2 = ChatPromptTemplate.fromTemplate('Answer this: {question}').pipe(
llm,
);
const result2 = await chain2.invoke(
{ question: result1.content },
{ callbacks: [callback] },
);

// Both operations are grouped together in the platform by threadId

The callback handler automatically traces:

  • LLM calls (with token usage)
  • Chain executions
  • Tool calls
  • Agent operations
  • Retriever operations
  • Parent-child span relationships
  • Errors and failures
  • Thread ID for conversation grouping

Privacy Mode​

Both plugins support a captureContent / capture_content option. Set it to false / False to disable capturing request and response content, while still collecting metadata, timing, and token usage:

trackOpenAI(openai, {
mentioraClient: client,
captureContent: false,
});

See Also​