Skip to main content

Examples

Complete, runnable example applications demonstrating how to use the Mentiora SDK.

Basic Tracing

Manual trace instrumentation with full control over trace events, parent-child spans, and conversation threading.

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

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

const result = await client.tracing.sendTrace({
traceId: uuidv7(),
spanId: uuidv7(),
threadId: uuidv7(),
name: 'llm.call',
type: 'llm',
input: { messages: [{ role: 'user', content: 'Hello!' }] },
output: { content: 'Hi there!' },
startTime: new Date(),
endTime: new Date(),
durationMs: 1200,
usage: { prompt_tokens: 10, completion_tokens: 8, total_tokens: 18 },
model: 'gpt-4o',
provider: 'openai',
});

Full source: examples/typescript/basic-tracing

OpenAI Integration

Automatic tracing of OpenAI API calls with zero code changes to your existing OpenAI usage.

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

const mentioraClient = new MentioraClient({
apiKey: process.env.MENTIORA_API_KEY,
});
const openaiClient = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

const trackedClient = trackOpenAI(openaiClient, {
mentioraClient,
tags: ['my-app'],
});

// Use trackedClient exactly like your regular OpenAI client
const response = await trackedClient.chat.completions.create({
model: 'gpt-5-mini',
messages: [{ role: 'user', content: 'Hello!' }],
});

Full source: examples/typescript/openai-integration

LangChain Integration

Automatic tracing of LangChain operations using a callback handler.

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

const mentioraClient = new MentioraClient({
apiKey: process.env.MENTIORA_API_KEY,
});
const callback = new MentioraTracingLangChain({
mentioraClient,
tags: ['my-app'],
});

const llm = new ChatOpenAI({ model: 'gpt-5-mini' });
const prompt = ChatPromptTemplate.fromTemplate('Tell me about {topic}');
const chain = prompt.pipe(llm);

const result = await chain.invoke({ topic: 'AI' }, { callbacks: [callback] });

Full source: examples/typescript/langchain-integration

Agent Run (Non-Streaming)

Run an agent and get the complete response in a single call.

const result = await client.agents.run({
tag: 'my-agent',
message: 'What is the weather in Paris?',
});
console.log(result.output);
console.log(`Thread: ${result.threadId}`);

Agent Streaming

Stream agent responses token-by-token for real-time output.

const stream = client.agents.stream({
tag: 'my-agent',
message: 'Tell me a story',
});
for await (const event of stream) {
if (event.type === 'output_text_delta') {
process.stdout.write(event.delta);
}
}

Multi-Turn Conversation

Continue a conversation across multiple messages by passing the threadId from the first response.

let threadId: string | undefined;

// First message
const first = await client.agents.run({
tag: 'assistant',
message: 'Hello!',
});
threadId = first.threadId;

// Follow-up (same thread)
const second = await client.agents.run({
tag: 'assistant',
message: 'What did I just say?',
threadId,
});

Full-Stack Chatbot

Complete chatbot applications with streaming UI, built with the SDK's streaming helpers:

Running an Example

cd examples/typescript/<example-name>
pnpm install
cp .env.example .env # Then edit .env with your keys
pnpm start

Environment Variables

VariableRequired ForDescription
MENTIORA_API_KEYAll examplesYour Mentiora API key — see Authentication
MENTIORA_BASE_URLAll examplesMentiora platform URL (defaults to https://platform.mentiora.ai)
OPENAI_API_KEYOpenAI & LangChain examplesYour OpenAI API key