llm-fallback-chain
v0.1.0
Published
LLM fallback chain with free providers for text and image generation
Readme
llm-fallback-chain
Private npm package. Provides a fallback chain across free LLM providers: Mistral → Groq → Gemini → OpenRouter
When one provider returns 429 / 503, the next one is tried automatically.
Install (from GitHub)
// package.json of your project
{
"dependencies": {
"llm-fallback-chain": "github:YOUR_USERNAME/llm-fallback-chain"
}
}npm installUsage: Next.js (App Router)
app/api/chat/route.ts
import { createLLMChain } from 'llm-fallback-chain/node'
const llm = createLLMChain({
providers: [
{ provider: 'mistral', apiKey: process.env.MISTRAL_API_KEY! },
{ provider: 'groq', apiKey: process.env.GROQ_API_KEY! },
{ provider: 'gemini', apiKey: process.env.GEMINI_API_KEY! },
{ provider: 'openrouter', apiKey: process.env.OPENROUTER_API_KEY! },
],
onFallback: (from, to, err) => {
console.warn(`[llm] Falling back ${from} → ${to}:`, err.message)
},
})
export const POST = llm.createNextHandler({
systemPrompt: 'You are a helpful assistant.',
})app/page.tsx
'use client'
import { LLMProvider, useLLMChat } from 'llm-fallback-chain/react'
function Chat() {
const { messages, send, loading } = useLLMChat()
return (
<div>
{messages.map((m, i) => <p key={i}><b>{m.role}:</b> {m.content}</p>)}
<button onClick={() => send('Hello!')} disabled={loading}>
{loading ? '...' : 'Send'}
</button>
</div>
)
}
export default function Page() {
return (
<LLMProvider endpoint="/api/chat">
<Chat />
</LLMProvider>
)
}Usage: Expo / React Native
Same as React — hooks work identically.
Just make sure endpoint points to your deployed API (not localhost in production).
<LLMProvider endpoint="https://your-api.vercel.app/api/chat">
<YourScreen />
</LLMProvider>Direct Node.js call (without HTTP)
import { createLLMChain } from 'llm-fallback-chain/node'
const llm = createLLMChain({ providers: [...] })
const result = await llm.call({
messages: [{ role: 'user', content: 'Tell me a joke' }],
systemPrompt: 'You are funny.',
temperature: 0.9,
})
console.log(result.content, result.provider)Build
npm install
npm run build # outputs to dist/Always commit dist/ — this is what gets installed in other projects.
Providers & Free Tiers
| Provider | Free tokens | Model (default) | |------------|------------------|----------------------------------------| | Mistral | 1B / month | mistral-small-latest | | Groq | rate-limited | llama3-8b-8192 | | Gemini | generous free | gemini-1.5-flash | | OpenRouter | multiple free | mistralai/mistral-7b-instruct:free |
