flexinference 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.0
4
+
5
+ Adds a typed **`PaymentRequiredError`** (a subclass of `FlexInferenceError`) thrown on
6
+ HTTP `402`, which the router returns when billing is past due and billable flex is paused
7
+ (free routing still works). Catch it on its own to prompt the user to update payment;
8
+ existing `catch (err instanceof FlexInferenceError)` handlers keep catching 402s because
9
+ it is a subclass. No other API or type changes.
10
+
3
11
  ## 1.2.0
4
12
 
5
13
  Adds the **Messages** caller format (`client.messages.create`), the fourth format
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # FlexInference (TypeScript)
2
2
 
3
- The official TypeScript SDK for [FlexInference](https://flexinference.com) - a deadline-aware, OpenAI-compatible inference router. Send the OpenAI requests you already send, bring your own OpenAI key, and set one required field - `start_within` - to trade latency for cost.
3
+ The official TypeScript SDK for [FlexInference](https://flexinference.com) - a deadline-aware inference router across **OpenAI, Google Gemini, and Anthropic**. Send the OpenAI requests you already send, bring your own provider key, and set one required field - `start_within` - to trade latency for cost. Four caller formats are supported: `responses`, `chat.completions`, `interactions` (Gemini shape), and `messages` (Anthropic shape) - any of them reaches any provider.
4
4
 
5
5
  ```bash
6
6
  npm install flexinference
@@ -26,14 +26,15 @@ Responses come back as the **raw OpenAI JSON** (we never reshape the body), so t
26
26
 
27
27
  `start_within` is **required** on every request. It takes `"default"`, `"priority"`, `"auto"`, or a duration `"HHh-MMm-SSs"` (5s-10m). The duration races OpenAI's flex tier on a flex-capable model and falls back to standard if it can't start in time; `"default"`, `"priority"`, and `"auto"` map to those OpenAI service tiers and proxy any model. It is typed, so a missing value or a value outside the allowed shapes is a **compile-time** error; a duration that fits the shape but is malformed or out of range is caught at **runtime**, the same check plain-JavaScript callers get. See the [docs](https://flexinference.mintlify.app/deadline-routing).
28
28
 
29
- ## Providers (OpenAI and Gemini)
29
+ ## Providers (OpenAI, Gemini, and Anthropic)
30
30
 
31
- FlexInference routes to **OpenAI** and **Google Gemini**. Send the same OpenAI-shaped request and pass whichever model id you want - `gpt-5.5`, `o4-mini`, `gemini-3.5-flash`, and so on. We run Gemini through its Interactions API and translate it back to the OpenAI shape, so your code is identical for both.
31
+ FlexInference routes to **OpenAI**, **Google Gemini**, and **Anthropic**. Send the same OpenAI-shaped request and pass whichever model id you want - `gpt-5.5`, `o4-mini`, `gemini-3.5-flash`, `claude-opus-4-8`, and so on. We translate Gemini and Anthropic to and from the OpenAI shape, so your code is identical for all three.
32
32
 
33
33
  - **OpenAI:** `default` (standard tier), `priority`, `auto`, and the flex race (a duration) on flex-capable models.
34
34
  - **Gemini:** `default` maps to Gemini's **standard** tier, plus `priority` and the flex race on the Gemini flex models (`gemini-3.5-flash`, `gemini-3.1-flash-lite`, `gemini-3.1-pro-preview`, `gemini-3-flash-preview`, `gemini-2.5-pro`, `gemini-2.5-flash`, `gemini-2.5-flash-lite`). Gemini has no `auto` tier, so `start_within: "auto"` on a Gemini model returns `400`.
35
+ - **Anthropic (Claude):** proxy-only. `default`, `priority`, and `auto` work; there is **no flex race**, so a duration `start_within` on a `claude-*` model returns `400 flex_unsupported_for_anthropic`. Anthropic requires a token cap, so set `max_output_tokens` (`max_completion_tokens` on Chat, `max_tokens` on Messages) or you get `400 missing_max_tokens`. You keep the unified API and tier control, and draw down your own Anthropic credits.
35
36
 
36
- Add the provider key you'll use (OpenAI and/or Gemini) in the [dashboard](https://www.flexinference.com/dashboard). Text, streaming, structured outputs, function calling, image input, and web search work on both providers (send a Responses `web_search` tool; we map it to Gemini's `google_search`).
37
+ Add the provider key you'll use (OpenAI, Gemini, and/or Anthropic) in the [dashboard](https://www.flexinference.com/dashboard). Text, streaming, structured outputs, function calling, image input, and web search work across providers (send a Responses `web_search` tool; we map it to Gemini's `google_search`).
37
38
 
38
39
  Don't send `service_tier` - the router controls the tier from `start_within` and rejects a caller-supplied `service_tier` with `400 service_tier_not_allowed`.
39
40
 
@@ -64,6 +65,39 @@ const res = await client.chat.completions.create({
64
65
  });
65
66
  ```
66
67
 
68
+ ## Interactions (Gemini shape)
69
+
70
+ Speak Google's Interactions shape and reach any model. `interactionText(res)` pulls the assistant text out of the interaction's `steps`.
71
+
72
+ ```ts
73
+ import { interactionText } from "flexinference";
74
+
75
+ const res = await client.interactions.create({
76
+ model: "gemini-3.5-flash",
77
+ input: "Summarize this contract.",
78
+ start_within: "00h-01m-00s",
79
+ });
80
+
81
+ console.log(interactionText(res));
82
+ ```
83
+
84
+ ## Messages (Anthropic shape)
85
+
86
+ Speak Anthropic's Messages shape and reach any model. `max_tokens` is required (Anthropic requires it). `messageText(res)` pulls the assistant text out of the message `content`.
87
+
88
+ ```ts
89
+ import { messageText } from "flexinference";
90
+
91
+ const res = await client.messages.create({
92
+ model: "claude-opus-4-8",
93
+ max_tokens: 1024,
94
+ messages: [{ role: "user", content: "Summarize this contract." }],
95
+ start_within: "default",
96
+ });
97
+
98
+ console.log(messageText(res));
99
+ ```
100
+
67
101
  ## Timeouts and cancellation
68
102
 
69
103
  Every client has a built-in timeout: **10 seconds to connect** and **600 seconds (10 min) total**, matching the router's budget, so a request can never hang forever. Override the total with the `timeout` option (raise it for very long streams):
@@ -102,6 +136,31 @@ try {
102
136
  }
103
137
  ```
104
138
 
139
+ ## Billing / 402
140
+
141
+ If your account's billing is past due, the router pauses **billable flex** and returns
142
+ `402 Payment Required` on those requests; free routing keeps working. The SDK throws a
143
+ typed `PaymentRequiredError` (a subclass of `FlexInferenceError`) for HTTP 402, so you
144
+ can catch it on its own and prompt the user to update payment while letting other errors
145
+ propagate:
146
+
147
+ ```ts
148
+ import { PaymentRequiredError } from "flexinference";
149
+
150
+ try {
151
+ await client.responses.create({ model: "gpt-5.5", input: "hi", start_within: "00h-00m-30s" });
152
+ } catch (err) {
153
+ if (err instanceof PaymentRequiredError) {
154
+ console.log("Billing is past due - update payment in the dashboard to resume flex.");
155
+ } else {
156
+ throw err;
157
+ }
158
+ }
159
+ ```
160
+
161
+ Because `PaymentRequiredError extends FlexInferenceError`, existing
162
+ `catch (err instanceof FlexInferenceError)` handlers keep catching 402s too.
163
+
105
164
  ## Configuration
106
165
 
107
166
  | Option | Default | Description |
package/dist/index.d.ts CHANGED
@@ -69,6 +69,13 @@ export declare class FlexInferenceError extends Error {
69
69
  readonly param: string | null | undefined;
70
70
  constructor(status: number, body: Partial<FlexErrorBody> | undefined, fallback: string);
71
71
  }
72
+ /**
73
+ * Thrown on HTTP 402: billing is past due, so billable flex is paused. Free routing
74
+ * still works. A subclass of FlexInferenceError, so existing `catch (err instanceof
75
+ * FlexInferenceError)` handlers keep working; narrow to this to handle 402 specially.
76
+ */
77
+ export declare class PaymentRequiredError extends FlexInferenceError {
78
+ }
72
79
  export interface ClientOptions {
73
80
  apiKey: string;
74
81
  /** Base URL of the FlexInference router. Defaults to the hosted endpoint. */
package/dist/index.js CHANGED
@@ -100,6 +100,13 @@ export class FlexInferenceError extends Error {
100
100
  this.param = err?.param;
101
101
  }
102
102
  }
103
+ /**
104
+ * Thrown on HTTP 402: billing is past due, so billable flex is paused. Free routing
105
+ * still works. A subclass of FlexInferenceError, so existing `catch (err instanceof
106
+ * FlexInferenceError)` handlers keep working; narrow to this to handle 402 specially.
107
+ */
108
+ export class PaymentRequiredError extends FlexInferenceError {
109
+ }
103
110
  const DEFAULT_BASE_URL = "https://api.flexinference.com/v1";
104
111
  const DEFAULT_TIMEOUT_MS = 600_000; // 10 min total budget, matching the router's generosity
105
112
  const CONNECT_TIMEOUT_MS = 10_000; // fail fast if the router is unreachable
@@ -218,6 +225,9 @@ export class FlexInference {
218
225
  finally {
219
226
  cleanup();
220
227
  }
228
+ if (res.status === 402) {
229
+ throw new PaymentRequiredError(res.status, parsed, `HTTP ${String(res.status)} ${res.statusText}`);
230
+ }
221
231
  throw new FlexInferenceError(res.status, parsed, `HTTP ${String(res.status)} ${res.statusText}`);
222
232
  }
223
233
  return { res, cleanup };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flexinference",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Official TypeScript SDK for FlexInference - a deadline-aware, OpenAI-compatible inference router.",
5
5
  "license": "MIT",
6
6
  "author": "Aditya Perswal <adityaperswal@gmail.com>",