TypeScript/JavaScript SDK for Tracia - store, test, and trace LLM prompts.
npm install tracia
# or
pnpm add tracia
# or
yarn add traciaimport { Tracia } from 'tracia';
const tracia = new Tracia({ apiKey: process.env.TRACIA_API_KEY });
const result = await tracia.prompts.run('welcome-email', {
name: 'Alice',
product: 'Tracia'
});
console.log(result.text);The main client class.
new Tracia(options: TraciaOptions)| Option | Type | Required | Description |
|---|---|---|---|
apiKey |
string |
Yes | Your Tracia API key (starts with tr_) |
baseUrl |
string |
No | API base URL (default: https://api.tracia.dev) |
Namespace for prompt operations (run, list, get, create, update, delete).
Runs a prompt and returns the generated text.
const result = await tracia.prompts.run(
'welcome-email', // prompt slug
{ name: 'Alice' }, // variables (optional)
{ model: 'gpt-4' } // options (optional)
);Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
slug |
string |
Yes | The prompt slug |
variables |
Record<string, string> |
No | Template variables |
options.model |
string |
No | Override the default model |
options.tags |
string[] |
No | Tags for filtering traces |
options.userId |
string |
No | End user identifier |
options.sessionId |
string |
No | Session identifier |
Returns: Promise<RunResult>
interface RunResult {
text: string; // The generated text
traceId: string; // Unique trace identifier
promptVersion: number; // Version of the prompt used
latencyMs: number; // Request latency in milliseconds
usage: {
inputTokens: number;
outputTokens: number;
totalTokens: number;
};
cost: number; // Cost in USD
}Lists all prompts for the authenticated user.
const prompts = await tracia.prompts.list();
console.log(prompts); // [{ slug: 'welcome-email', name: 'Welcome Email', ... }]Returns: Promise<PromptListItem[]>
Gets a single prompt with its current version content.
const prompt = await tracia.prompts.get('welcome-email');
console.log(prompt.content); // Array of messages
console.log(prompt.variables); // ['name', 'product']Returns: Promise<Prompt>
Creates a new prompt.
const prompt = await tracia.prompts.create({
name: 'Welcome Email',
slug: 'welcome-email', // optional, auto-generated from name if not provided
description: 'A welcome email template',
content: [
{ id: 'msg_1', role: 'system', content: 'You are a helpful assistant.' },
{ id: 'msg_2', role: 'user', content: 'Write a welcome email for {{name}}.' },
],
});Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
name |
string |
Yes | Display name for the prompt |
slug |
string |
No | URL-friendly identifier (auto-generated if not provided) |
description |
string |
No | Description of the prompt |
content |
PromptMessage[] |
Yes | Array of messages (system, user, assistant) |
Returns: Promise<Prompt>
Updates an existing prompt. If content changes, creates a new version.
const prompt = await tracia.prompts.update('welcome-email', {
name: 'Updated Welcome Email',
content: [
{ id: 'msg_1', role: 'user', content: 'New content for {{name}}.' },
],
});
console.log(prompt.currentVersion); // Version incremented if content changedReturns: Promise<Prompt>
Deletes a prompt and all its versions.
await tracia.prompts.delete('welcome-email');Returns: Promise<void>
interface Prompt {
id: string;
slug: string;
name: string;
description: string | null;
model: string | null;
currentVersion: number;
content: PromptMessage[];
variables: string[];
createdAt: string;
updatedAt: string;
}
interface PromptMessage {
id: string;
role: 'system' | 'user' | 'assistant';
content: string;
}
interface PromptListItem {
id: string;
slug: string;
name: string;
description: string | null;
model: string | null;
currentVersion: number;
variables: string[];
createdAt: string;
updatedAt: string;
}All API errors throw TraciaError with a specific error code:
import { Tracia, TraciaError, TraciaErrorCode } from 'tracia';
const tracia = new Tracia({ apiKey: process.env.TRACIA_API_KEY });
try {
const result = await tracia.prompts.run('welcome-email', { name: 'Alice' });
console.log(result.text);
} catch (error) {
if (error instanceof TraciaError) {
switch (error.code) {
case TraciaErrorCode.UNAUTHORIZED:
console.error('Invalid API key');
break;
case TraciaErrorCode.NOT_FOUND:
console.error('Prompt does not exist');
break;
case TraciaErrorCode.CONFLICT:
console.error('Resource already exists');
break;
case TraciaErrorCode.MISSING_VARIABLES:
console.error('Missing required template variables');
break;
case TraciaErrorCode.PROVIDER_ERROR:
console.error('LLM provider error:', error.message);
break;
case TraciaErrorCode.NETWORK_ERROR:
console.error('Network error:', error.message);
break;
case TraciaErrorCode.TIMEOUT:
console.error('Request timed out');
break;
default:
console.error('Unknown error:', error.message);
}
}
}| Code | Description |
|---|---|
UNAUTHORIZED |
Invalid or missing API key |
NOT_FOUND |
The requested resource (prompt, etc.) does not exist |
CONFLICT |
Resource already exists (e.g., duplicate slug) |
MISSING_VARIABLES |
Required template variables are missing |
MISSING_PROVIDER_KEY |
No LLM provider key configured |
PROVIDER_ERROR |
Error from the LLM provider (OpenAI, etc.) |
INVALID_REQUEST |
Invalid request format |
NETWORK_ERROR |
Network connectivity error |
TIMEOUT |
Request timed out (30 second limit) |
The SDK is written in TypeScript and exports all types:
import {
Tracia,
TraciaOptions,
RunOptions,
RunResult,
TokenUsage,
Prompt,
PromptListItem,
PromptMessage,
MessageRole,
CreatePromptOptions,
UpdatePromptOptions,
TraciaError,
TraciaErrorCode,
} from 'tracia';- Node.js 18+ (uses native
fetch) - Works in modern browsers with native
fetchsupport
MIT