Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

feat(ai-gateway): abuse-investigation logger + stream-error visibility

Louis Beaumont94e161d5d5e1
diff --git a/packages/ai-gateway/src/index.ts b/packages/ai-gateway/src/index.ts
index aca05a7c4..5aaa5f922 100644
--- a/packages/ai-gateway/src/index.ts
+++ b/packages/ai-gateway/src/index.ts
@@ -98,6 +98,26 @@ async function handleRequest(request: Request, env: Env, ctx: ExecutionContext):
})));
}
+ // Temp abuse-investigation logger: when DEBUG_USER_ID matches the caller,
+ // dump request body + headers + CF metadata to Workers Logs. The user_id
+ // is NEVER printed — chunks are tagged with a random id only.
+ if (env.DEBUG_USER_ID && authResult.userId === env.DEBUG_USER_ID) {
+ const cf = (request as any).cf || {};
+ const payload = JSON.stringify({
+ ts: new Date().toISOString(),
+ ip: request.headers.get('cf-connecting-ip'),
+ ua: request.headers.get('user-agent'),
+ cf: { country: cf.country, colo: cf.colo, asn: cf.asOrganization, city: cf.city },
+ body,
+ });
+ const tag = crypto.randomUUID().slice(0, 8);
+ const CHUNK = 20000;
+ const total = Math.ceil(payload.length / CHUNK) || 1;
+ for (let i = 0, p = 1; i < payload.length; i += CHUNK, p++) {
+ console.log(`[ABUSE-DEBUG ${tag} ${p}/${total}] ${payload.slice(i, i + CHUNK)}`);
+ }
+ }
+
// Check if model is allowed for this tier
if (!isModelAllowed(body.model, authResult.tier, env)) {
const allowedModels = getTierConfig(env)[authResult.tier].allowedModels;
diff --git a/packages/ai-gateway/src/providers/anthropic.ts b/packages/ai-gateway/src/providers/anthropic.ts
index 16f8a1006..4309c70be 100644
--- a/packages/ai-gateway/src/providers/anthropic.ts
+++ b/packages/ai-gateway/src/providers/anthropic.ts
@@ -263,6 +263,9 @@ export class AnthropicProvider implements AIProvider {
// Send error as SSE event so the client can parse it instead of a broken stream
const errorMessage = error?.message || 'Unknown streaming error';
const errorStatus = error?.status || 500;
+ // Visibility: surface upstream Anthropic streaming failures to Workers Logs.
+ // Without this, in-stream errors hide behind HTTP 200 and look like success.
+ console.warn(`[anthropic-stream-error] status=${errorStatus} type=${error?.error?.type || error?.name || 'unknown'} msg=${String(errorMessage).slice(0, 500)}`);
try {
controller.enqueue(
new TextEncoder().encode(
diff --git a/packages/ai-gateway/src/types.ts b/packages/ai-gateway/src/types.ts
index cdbdd8c78..e865f72d8 100644
--- a/packages/ai-gateway/src/types.ts
+++ b/packages/ai-gateway/src/types.ts
@@ -187,6 +187,9 @@ export interface Env {
LIMIT_SUBSCRIBED_DAILY?: string;
LIMIT_SUBSCRIBED_RPM?: string;
LIMIT_IP_DAILY?: string;
+ // Temporary abuse-investigation: if set, log raw request body + headers
+ // when authResult.userId matches. Never log the user_id itself.
+ DEBUG_USER_ID?: string;
}
// User tier for rate limiting and model access