Skip to content

Quick Start

Get started with ai.matey in less than 5 minutes. This guide will walk you through creating your first bridge and making AI requests.

Terminal window
npm install ai.matey.core ai.matey.frontend ai.matey.backend

You’ll need an API key from at least one provider. We recommend starting with Anthropic (Claude) as it’s reliable and has good free tier:

Terminal window
# Sign up at https://console.anthropic.com/
# Get your API key and set it in your environment
export ANTHROPIC_API_KEY=sk-ant-...

Alternatively, use any of these providers:

Create a file called hello.ts:

import { Bridge } from 'ai.matey.core';
import { OpenAIFrontendAdapter } from 'ai.matey.frontend/openai';
import { AnthropicBackendAdapter } from 'ai.matey.backend/anthropic';
// Create a bridge: OpenAI format → Anthropic execution
const bridge = new Bridge(
new OpenAIFrontendAdapter(),
new AnthropicBackendAdapter({
apiKey: process.env.ANTHROPIC_API_KEY
})
);
// Make a request using OpenAI format
const response = await bridge.chat({
model: 'gpt-4', // Will be mapped to claude-3-5-sonnet
messages: [
{ role: 'user', content: 'What is ai.matey?' }
]
});
console.log(response.choices[0].message.content);
Terminal window
npx tsx hello.ts

You should see Claude’s response, even though you wrote the code in OpenAI format!

Your Code (OpenAI format)
OpenAI Frontend Adapter (parses OpenAI format)
Intermediate Representation (IR) - Universal format
Anthropic Backend Adapter (converts IR to Anthropic format)
Claude API (executes the request)
Response (converted back through the pipeline)

This is the power of ai.matey: write once, run anywhere.

For simple use cases, use backends directly:

import { AnthropicBackendAdapter } from 'ai.matey.backend/anthropic';
const backend = new AnthropicBackendAdapter({
apiKey: process.env.ANTHROPIC_API_KEY
});
// Execute using Intermediate Representation (IR) format
const response = await backend.execute({
model: 'claude-3-5-sonnet-20241022',
messages: [
{ role: 'user', content: 'Hello!' }
]
});

Stream responses in real-time:

const stream = await bridge.chatStream({
model: 'gpt-4',
messages: [
{ role: 'user', content: 'Write a haiku about TypeScript' }
],
stream: true
});
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta?.content;
if (delta) {
process.stdout.write(delta);
}
}

Pattern 3: Multiple Providers with Fallback

Section titled “Pattern 3: Multiple Providers with Fallback”

Route to multiple providers with automatic fallback:

import { Bridge, createRouter } from 'ai.matey.core';
import { OpenAIFrontendAdapter } from 'ai.matey.frontend/openai';
import { OpenAIBackendAdapter } from 'ai.matey.backend/openai';
import { AnthropicBackendAdapter } from 'ai.matey.backend/anthropic';
const router = createRouter({
routingStrategy: 'fallback'
})
.register('openai', new OpenAIBackendAdapter({
apiKey: process.env.OPENAI_API_KEY
}))
.register('anthropic', new AnthropicBackendAdapter({
apiKey: process.env.ANTHROPIC_API_KEY
}))
.setFallbackChain(['openai', 'anthropic']); // Try OpenAI first, fall back to Anthropic
const bridge = new Bridge(
new OpenAIFrontendAdapter(),
router
);
// If OpenAI fails, automatically tries Anthropic
const response = await bridge.chat({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }]
});

Add logging, caching, or retry logic:

import { Bridge } from 'ai.matey.core';
import { OpenAIFrontendAdapter } from 'ai.matey.frontend/openai';
import { AnthropicBackendAdapter } from 'ai.matey.backend/anthropic';
import { createLoggingMiddleware, createRetryMiddleware } from 'ai.matey.middleware';
const bridge = new Bridge(
new OpenAIFrontendAdapter(),
new AnthropicBackendAdapter({ apiKey: process.env.ANTHROPIC_API_KEY }),
[
createLoggingMiddleware({ level: 'info' }),
createRetryMiddleware({ maxAttempts: 3 })
]
);
// Now all requests are logged and automatically retried on failure
const response = await bridge.chat({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }]
});

The beauty of ai.matey is that switching providers is trivial. Just change the backend:

// Development: Use local Ollama
const devBackend = new OllamaBackendAdapter({
baseURL: 'http://localhost:11434'
});
// Production: Use OpenAI
const prodBackend = new OpenAIBackendAdapter({
apiKey: process.env.OPENAI_API_KEY
});
// Choose based on environment
const backend = process.env.NODE_ENV === 'production'
? prodBackend
: devBackend;
const bridge = new Bridge(
new OpenAIFrontendAdapter(),
backend
);
// Same code works with both!
const response = await bridge.chat({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }]
});

You can also swap the frontend to use different API formats:

import { AnthropicFrontendAdapter } from 'ai.matey.frontend/anthropic';
import { OpenAIBackendAdapter } from 'ai.matey.backend/openai';
// Accept Anthropic format → Execute on OpenAI
const bridge = new Bridge(
new AnthropicFrontendAdapter(),
new OpenAIBackendAdapter({ apiKey: process.env.OPENAI_API_KEY })
);
// Now use Anthropic's API format
const response = await bridge.chat({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 100,
messages: [
{ role: 'user', content: 'Hello!' }
]
});
Error: Missing required API key

Solution: Set your API key in the environment:

Terminal window
export ANTHROPIC_API_KEY=sk-ant-...

Or load from a .env file using dotenv:

Terminal window
npm install dotenv
import 'dotenv/config';
Cannot find module 'ai.matey.core'

Solution: Make sure you’ve installed all required packages:

Terminal window
npm install ai.matey.core ai.matey.frontend ai.matey.backend

By default, models are automatically mapped between providers:

  • gpt-4claude-3-5-sonnet-20241022 (OpenAI → Anthropic)
  • gpt-3.5-turboclaude-3-haiku-20240307 (OpenAI → Anthropic)

To use specific models, check the Backend Adapters documentation.


Ready to build? Continue to Core Concepts to learn the fundamentals, or jump straight to Your First Bridge for a hands-on tutorial!