Edge-ready, multi-provider LLM orchestrator with streaming, caching, and Zod-powered structured output.
Most LLM SDKs are heavy, Node-only, or lock you into a single provider. Universal LLM is built for the modern web:
- π Edge-Ready β 100%
fetch-based. Works on Vercel Edge, Cloudflare Workers, Bun, Deno, and the Browser. - π Multi-Provider β OpenAI, Anthropic, Grok (xAI), and Ollama with a single, unified API.
- β Zod-Powered β Guaranteed structured output. Define a schema, get a typed object back.
- π‘ Streaming β Native SSE streaming for all providers via
AsyncGenerator. - πΎ Caching β Built-in in-memory response cache with configurable TTL.
- π Retries β Automatic exponential backoff on failures.
- πͺ Middleware β
onRequest/onResponse/onErrorhooks for logging, PII scrubbing, or transformation. - πͺΆ Ultra-Lightweight β Zero runtime dependencies (only
zodas a peer dependency).
npm install universal-llm zodimport { UniversalLLM } from 'universal-llm';
const llm = new UniversalLLM({
provider: 'openai',
apiKey: process.env.OPENAI_API_KEY,
});
const response = await llm.chat([
{ role: 'user', content: 'What is the future of AI?' }
]);
console.log(response);const llm = new UniversalLLM({
provider: 'anthropic', // or 'grok', 'ollama'
apiKey: process.env.ANTHROPIC_API_KEY,
});Define your data shape and get a fully-typed, validated JavaScript object back.
import { z } from 'zod';
import { UniversalLLM } from 'universal-llm';
const UserSchema = z.object({
name: z.string(),
age: z.number(),
interests: z.array(z.string()),
});
const llm = new UniversalLLM({ provider: 'openai', apiKey: '...' });
const user = await llm.structured({
schema: UserSchema,
prompt: 'Extract: John is 25, loves surfing and AI.',
});
// user is fully typed as { name: string; age: number; interests: string[] }
console.log(user.name); // "John"const llm = new UniversalLLM({ provider: 'openai', apiKey: '...' });
for await (const chunk of llm.stream([
{ role: 'user', content: 'Write a haiku about code.' }
])) {
process.stdout.write(chunk); // Real-time output
}const llm = new UniversalLLM({
provider: 'openai',
apiKey: '...',
cache: true,
cacheTTL: 60000, // 1 minute
});
// First call hits the API
await llm.chat([{ role: 'user', content: 'Hello' }]);
// Second identical call returns from cache instantly
await llm.chat([{ role: 'user', content: 'Hello' }]);
// Manually clear if needed
llm.clearCache();const llm = new UniversalLLM({
provider: 'openai',
apiKey: '...',
retries: 3, // Retry up to 3 times
retryDelay: 1000, // Start with 1s, then 2s, then 4s
timeout: 30000, // Abort after 30 seconds
});const llm = new UniversalLLM({
provider: 'openai',
apiKey: '...',
onRequest: (payload) => {
console.log('Sending:', payload);
return payload; // Optionally modify
},
onResponse: (text) => {
console.log('Received:', text.length, 'chars');
return text; // Optionally transform
},
onError: (err) => {
console.error('LLM Error:', err.message);
},
});| Option | Type | Default | Description |
|---|---|---|---|
provider |
'openai' | 'anthropic' | 'grok' | 'ollama' |
required | LLM provider |
apiKey |
string |
'' |
API key (not needed for Ollama) |
baseUrl |
string |
Provider default | Custom API endpoint |
timeout |
number |
30000 |
Request timeout in ms |
retries |
number |
2 |
Number of retry attempts |
retryDelay |
number |
1000 |
Initial retry delay in ms |
cache |
boolean |
false |
Enable response caching |
cacheTTL |
number |
300000 |
Cache time-to-live in ms |
onRequest |
function |
β | Middleware before sending |
onResponse |
function |
β | Middleware after receiving |
onError |
function |
β | Error handler hook |
| Method | Returns | Description |
|---|---|---|
chat(messages, options?) |
Promise<string> |
Standard completion |
stream(messages, options?) |
AsyncGenerator<string> |
Streaming completion |
structured(options) |
Promise<z.infer<T>> |
Zod-validated structured output |
clearCache() |
void |
Clear the response cache |
| Provider | Default Model | Streaming | Edge Support |
|---|---|---|---|
| OpenAI | gpt-4o |
β | β |
| Anthropic | claude-sonnet-4-20250514 |
β | β |
| Grok (xAI) | grok-3 |
β | β |
| Ollama | llama3 |
β | β (Local) |
MIT Β© Antigravity