You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add openrouter configs in default models
* Improve metadata and logging for better debugging
* Add openrouter in react playground example
* Fix fallback logic - retry a provider only when it is configured properly i.e. has api key if auth mandatory
* Update docs and readme
-**Automatic Token Counting**: Accurate token estimation for every request, no manual calculation needed
@@ -33,7 +33,7 @@ npm i resilient-llm
33
33
import { ResilientLLM } from'resilient-llm';
34
34
35
35
constllm=newResilientLLM({
36
-
aiService:'openai', // or 'anthropic', 'google', 'ollama'
36
+
aiService:'openai', // or 'anthropic', 'google', 'openrouter', 'ollama'
37
37
model:'gpt-5-nano',
38
38
maxTokens:2048,
39
39
temperature:0.7,
@@ -151,7 +151,7 @@ For all supported shapes (including plain schema objects) and parsing/validation
151
151
152
152
## Supported LLM Providers
153
153
154
-
ResilientLLM comes with built-in support for all text chat completiion models provided by **OpenAI**, **Anthropic**, **Google/Gemini**, **Ollama** API, etc.
154
+
ResilientLLM comes with built-in support for all text chat completion models provided by **OpenAI**, **Anthropic**, **Google/Gemini**, **OpenRouter**, and **Ollama** APIs.
155
155
156
156
**Adding custom providers:** You can add support for other LLM providers (e.g., Together AI, Groq, self-hosted vLLM, or any OpenAI/Anthropic-compatible API) using `ProviderRegistry.configure()`. See the [Custom Provider Guide](./docs/custom-providers.md) for detailed instructions and examples.
157
157
@@ -164,11 +164,41 @@ The simplest way is using environment variables:
164
164
exportOPENAI_API_KEY=sk-your-key-here
165
165
exportANTHROPIC_API_KEY=sk-ant-your-key-here
166
166
exportGOOGLE_API_KEY=your-key-here
167
+
exportOPENROUTER_API_KEY=your-key-here
167
168
exportOLLAMA_API_KEY=your-key-here
168
169
```
169
170
170
171
For more ways to configure API key, see the [API Key Configuration guide](./docs/reference.md#api-key-configuration) in the reference documentation.
171
172
173
+
## Free LLM API Setup
174
+
175
+
You can try ResilientLLM without paid provider keys using [OpenRouter](https://openrouter.ai/) free models. OpenRouter routes requests to free models (with rate limits); no credit card is required to sign up.
176
+
177
+
1. Sign up at [openrouter.ai](https://openrouter.ai/) (email or GitHub).
178
+
2. Open **Keys** in the dashboard ([openrouter.ai/keys](https://openrouter.ai/keys)).
179
+
3. Click **Create Key**, give it a name, and copy the key (starts with `sk-or-`).
180
+
181
+
You can then use `openrouter/free` model which randomly selects a free model.
182
+
183
+
184
+
```javascript
185
+
import { ResilientLLM } from'resilient-llm';
186
+
187
+
constllm=newResilientLLM({
188
+
aiService:'openrouter',
189
+
model:'openrouter/free',
190
+
apiKey:'sk-or-***'
191
+
});
192
+
193
+
const { content } =awaitllm.chat([
194
+
{ role:'user', content:'What is the capital of France?' }
195
+
]);
196
+
197
+
console.log(content);
198
+
```
199
+
200
+
The output might be extremely poor with `openrouter/free` model. If so, try a specific [free model from this list](https://openrouter.ai/models?max_price=0&output_modalities=text) or use a one of the [popular paid models](https://openrouter.ai/models?output_modalities=text&order=most-popular).
201
+
172
202
## Examples and Playground
173
203
174
204
Complete working projects using Resilient LLM as core library to call LLM APIs with resilience.
Copy file name to clipboardExpand all lines: docs/reference.md
+27-4Lines changed: 27 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ Complete technical reference for the ResilientLLM library API.
18
18
19
19
## ResilientLLM
20
20
21
-
A unified interface for interacting with multiple LLM providers (OpenAI, Anthropic, Google/Gemini, Ollama) with built-in resilience features including rate limiting, retries, circuit breakers, and error handling.
21
+
A unified interface for interacting with multiple LLM providers (OpenAI, Anthropic, Google/Gemini, OpenRouter, Ollama) with built-in resilience features including rate limiting, retries, circuit breakers, and error handling.
22
22
23
23
### ResilientLLM Constructor
24
24
@@ -39,7 +39,7 @@ new ResilientLLM(options?: ResilientLLMOptions)
|`aiService`|`string`| No |`process.env.PREFERRED_AI_SERVICE` or `"anthropic"`| AI service provider: `"openai"`, `"anthropic"`, `"google"`, or `"ollama"`|
42
+
|`aiService`|`string`| No |`process.env.PREFERRED_AI_SERVICE` or `"anthropic"`| AI service provider: `"openai"`, `"anthropic"`, `"google"`, `"openrouter"`, or `"ollama"`|
43
43
|`model`|`string`| No |`process.env.PREFERRED_AI_MODEL` or `"claude-3-5-sonnet-20240620"`| Model identifier for the selected AI service |
44
44
|`temperature`|`number`| No |`process.env.AI_TEMPERATURE` or `0`| Temperature parameter (0-2) controlling randomness in responses |
45
45
|`maxTokens`|`number`| No |`process.env.MAX_TOKENS` or `2048`| Maximum number of tokens in the response |
If `error` is already a **`ResilientLLMError`**, it is rethrown (metadata may be merged). Otherwise **`statusCode`** selects a **`PROVIDER_*`** code (e.g. `401` → `PROVIDER_UNAUTHORIZED`); `null` or unknown statuses map to **`PROVIDER_ERROR`**. See [`lib/ResilientLLMError.ts`](../lib/ResilientLLMError.ts) for the full `ResilientLLMErrorCode` union.
309
+
| Status Code | Error Message |
310
+
|------------|---------------|
311
+
| `400` | "Bad request" |
312
+
| `401` | "Invalid API Key" |
313
+
| `403` | "You are not authorized to access this resource" |
314
+
| `404` | "Not found" |
315
+
| `429` | "Rate limit exceeded" |
316
+
| `500` | "Internal server error" |
317
+
| `503` | "Service unavailable" |
318
+
| `529` | "API temporarily overloaded" |
319
+
| Other | "Unknown error" |
320
+
321
+
**Note:** This method is called internally by the `chat()` method when errors occur. You typically don't need to call it directly.
310
322
311
323
---
312
324
@@ -784,6 +796,7 @@ Set at least one API key for your chosen service:
784
796
| `OPENAI_API_KEY` | OpenAI | Yes (if using OpenAI) |
785
797
| `ANTHROPIC_API_KEY` | Anthropic | Yes (if using Anthropic) |
786
798
| `GOOGLE_API_KEY` or `GOOGLE_GENERATIVE_AI` or `GEMINI_API_KEY` | Google | Yes (if using Google) |
799
+
| `OPENROUTER_API_KEY` | OpenRouter | Yes (if using OpenRouter) |
787
800
| `OLLAMA_API_KEY` | Ollama | No (optional) |
788
801
789
802
**Note:** For custom providers, use the environment variable names specified in `ProviderRegistry.configure()` via `envVarNames`.
@@ -800,6 +813,8 @@ Set at least one API key for your chosen service:
800
813
| `MAX_INPUT_TOKENS` | `100000` | Default max input tokens |
801
814
| `AI_TOP_P` | `0.95` | Default top-p value |
802
815
| `OLLAMA_API_URL` | `"http://localhost:11434/api/generate"` | Ollama API URL |
0 commit comments