Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(ai): release the reservation of documented-unbilled embedding rejections

garrytanebd9e6ec7229
diff --git a/src/core/ai/gateway.ts b/src/core/ai/gateway.ts
index 5ad788eb58..f0c9f3b5fe 100644
--- a/src/core/ai/gateway.ts
+++ b/src/core/ai/gateway.ts
@@ -1620,6 +1620,27 @@ export async function embed(texts: string[], opts?: EmbedOpts): Promise<Float32A
}
}
+/**
+ * Embedding providers whose own documentation says a rejected request is not
+ * billed. Only for these does a permanent request-shaped rejection (HTTP 400,
+ * 413 or 422; never 401/403/429) release its invocation reservation instead of
+ * keeping the maximum debit. Verified per provider:
+ * - google: "If your request fails with a 400 or 500 error, you won't be
+ * charged for the tokens used." (ai.google.dev/gemini-api/docs/billing,
+ * "Am I charged for failed requests?", checked 2026-10-01)
+ * OpenAI, Voyage and the other embedding recipes publish no such statement, so
+ * their rejections keep the debit (token-limit rejections are handled above).
+ */
+const UNBILLED_REJECTION_PROVIDERS: ReadonlySet<string> = new Set(['google']);
+
+/** @internal exported for tests; not part of the public gateway API. */
+export function isUnbilledEmbeddingRejection(recipeId: string, err: unknown): boolean {
+ if (!UNBILLED_REJECTION_PROVIDERS.has(recipeId)) return false;
+ const e = err as { statusCode?: unknown; status?: unknown } | null;
+ const status = typeof e?.statusCode === 'number' ? e.statusCode : typeof e?.status === 'number' ? e.status : undefined;
+ return status === 400 || status === 413 || status === 422;
+}
+
/**
* Returns true if the error looks like a provider batch-token-limit error.
*
@@ -1710,7 +1731,7 @@ async function embedSubBatch(
// deadline) — shorter wins.
abortSignal: withDefaultTimeout(opts?.abortSignal, AI_EMBED_TIMEOUT_MS),
...(hasAIInvocationGuard() ? { maxRetries: 0 } : opts?.maxRetries !== undefined ? { maxRetries: opts.maxRetries } : {}),
- }), sdkInvocationUsage, err => isTokenLimitError(err) ? { inputTokens: 0, outputTokens: 0 } : null);
+ }), sdkInvocationUsage, err => isTokenLimitError(err) || isUnbilledEmbeddingRejection(recipe.id, err) ? { inputTokens: 0, outputTokens: 0 } : null);
// Carry the threaded input_type across the SDK boundary via
// __embedInputTypeStore (the adapter strips it from providerOptions —
// see the store's doc comment). Populated only when dimsProviderOptions
diff --git a/test/embedding-rejection-refund.serial.test.ts b/test/embedding-rejection-refund.serial.test.ts
new file mode 100644
index 0000000000..5a1a562415
--- /dev/null
+++ b/test/embedding-rejection-refund.serial.test.ts
@@ -0,0 +1,88 @@
+/**
+ * Wave-3 connector follow-up #11: an embedding request the provider rejects
+ * (HTTP 400/413/422) kept its maximum debit against the `gbrain migrate
+ * embeddings --max-cost-usd` authorization, so a migration could stop early at
+ * the cap. Only token-limit rejections released their reservation. A rejection
+ * is now released only for providers whose documentation says rejected
+ * requests are not billed (Google); every other provider keeps the debit.
+ *
+ * Each case runs at exactly the cap: the authorization covers one request at
+ * its maximum input size, so the request after a rejection is admitted only if
+ * the rejection was released.
+ *
+ * Serial: the gateway transport and configuration are process-global.
+ */
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { configureGateway, embed, resetGateway, __setEmbedTransportForTests, isUnbilledEmbeddingRejection } from '../src/core/ai/gateway.ts';
+import { planEmbeddingMigration, readMigrationState, runSchemaTransition } from '../src/core/embedding-migration.ts';
+import { authorizeMigrationBudget } from '../src/core/embedding-migration-budget.ts';
+import { withAIInvocationGuard, type AIInvocation } from '../src/core/ai/invocation-guard.ts';
+
+const RATE_PER_MTOK = 1_000;
+const TEXT = 'Synthetic rejected chunk. '.repeat(20);
+
+let engine: PGLiteEngine;
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+}, 60_000);
+
+afterAll(async () => {
+ __setEmbedTransportForTests(null);
+ resetGateway();
+ await engine.disconnect();
+}, 60_000);
+
+function rejection(status: number): Error {
+ return Object.assign(new Error('Synthetic invalid request: input rejected'), { statusCode: status });
+}
+
+async function atExactCap(model: string, dims: number, env: Record<string, string>, status: number) {
+ await runSchemaTransition(engine, dims);
+ await engine.executeRaw("DELETE FROM config WHERE key LIKE 'embedding_migration.%'");
+ await engine.setConfig('pricing.overrides', JSON.stringify({ [model]: RATE_PER_MTOK }));
+ resetGateway();
+ configureGateway({ embedding_model: model, embedding_dimensions: dims, env });
+ const vector = () => Array.from({ length: dims }, () => 0.1);
+ let reject = true;
+ __setEmbedTransportForTests(async ({ values }: { values: string[] }) => {
+ if (reject) throw rejection(status);
+ return { values, warnings: [], embeddings: values.map(vector), usage: { tokens: 10 } };
+ });
+ // The cap is exactly one request at its maximum input size.
+ let maxInputTokens = 0;
+ await withAIInvocationGuard(async (call: AIInvocation) => { maxInputTokens = call.maxInputTokens!; return { settle: async () => {} }; },
+ () => embed([TEXT], { embeddingModel: model, dimensions: dims })).catch(() => {});
+ expect(maxInputTokens).toBeGreaterThan(0);
+ const cap = maxInputTokens * RATE_PER_MTOK / 1_000_000;
+ const debit = await authorizeMigrationBudget(engine, await planEmbeddingMigration(engine, { to: model, dim: dims }), cap);
+ await expect(withAIInvocationGuard(debit, () => embed([TEXT], { embeddingModel: model, dimensions: dims }))).rejects.toThrow();
+ const afterRejection = (await readMigrationState(engine)).state!.budget!;
+ reject = false;
+ const next = await withAIInvocationGuard(debit, () => embed([TEXT], { embeddingModel: model, dimensions: dims })).then(() => 'embedded', (e: Error) => e.message);
+ return { cap, afterRejection, next };
+}
+
+describe('embedding rejection refund', () => {
+ test('Google (documented unbilled): a 400 rejection is released and the next request fits the exact cap', async () => {
+ const r = await atExactCap('google:gemini-embedding-001', 768, { GOOGLE_GENERATIVE_AI_API_KEY: 'synthetic-only' }, 400);
+ expect(r.afterRejection.debited_usd).toBe(0);
+ expect(r.next).toBe('embedded');
+ });
+
+ test('OpenAI (no such statement): a 400 rejection keeps its debit and the next request is refused at the cap', async () => {
+ const r = await atExactCap('openai:text-embedding-3-small', 8, { OPENAI_API_KEY: 'synthetic-only' }, 400);
+ expect(r.afterRejection.debited_usd).toBeCloseTo(r.cap, 12);
+ expect(r.next).toContain('Migration authorization exhausted');
+ });
+
+ test('only request-shaped rejections from documented providers qualify', () => {
+ for (const status of [400, 413, 422]) expect(isUnbilledEmbeddingRejection('google', rejection(status))).toBe(true);
+ for (const status of [401, 403, 429, 500]) expect(isUnbilledEmbeddingRejection('google', rejection(status))).toBe(false);
+ for (const provider of ['openai', 'voyage', 'mistral', 'azure-openai']) expect(isUnbilledEmbeddingRejection(provider, rejection(400))).toBe(false);
+ expect(isUnbilledEmbeddingRejection('google', new Error('no status'))).toBe(false);
+ });
+});