Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
log codex request sizes
Raine Virtab471d16d7d2f
diff --git a/src/providers/codex/client.ts b/src/providers/codex/client.tsindex f5cc924..935010b 100644--- a/src/providers/codex/client.ts+++ b/src/providers/codex/client.ts@@ -7,6 +7,7 @@ import type { Logger } from "../../log.ts";import type { RequestContext } from "../types.ts";import type { ResponsesRequest } from "./translate/request.ts";import { retryOn429, sleep } from "../retry.ts";+import { summarizeCodexRequestSize } from "./request-summary.ts";const FETCH_WATCHDOG_INTERVAL_MS = 30_000;let fetchHeaderTimeoutMs = 60_000;@@ -54,7 +55,8 @@ async function retryHeaderTimeouts(try {return await run();} catch (err) {- if (!(err instanceof CodexHeaderTimeoutError) || attempt >= fetchHeaderTimeoutRetries) throw err;+ if (!(err instanceof CodexHeaderTimeoutError) || attempt >= fetchHeaderTimeoutRetries)+ throw err;const waitMs = fetchHeaderTimeoutMs <= 10 ? 0 : 500 + Math.round(Math.random() * 1000);log.warn("codex response headers timed out, retrying", {attempt: attempt + 1,@@ -64,6 +66,7 @@ async function retryHeaderTimeouts(model: body.model,inputCount: body.input.length,toolCount: body.tools?.length ?? 0,+ requestSize: summarizeCodexRequestSize(body),});await sleep(waitMs, signal);}@@ -136,11 +139,18 @@ async function doFetch(const codexUrl = codexBaseUrl(CODEX_API_ENDPOINT);+ const bodyJson = JSON.stringify(body);+ const size = summarizeCodexRequestSize(body, bodyJson);+log.debug("posting to codex", {url: codexUrl,model: body.model,inputCount: body.input.length,toolCount: body.tools?.length ?? 0,+ serviceTier: body.service_tier,+ reasoningEffort: body.reasoning?.effort,+ promptCacheKey: body.prompt_cache_key,+ size,});const startedAt = Date.now();@@ -162,7 +172,7 @@ async function doFetch(const resp = await fetch(codexUrl, {method: "POST",headers,- body: JSON.stringify(body),+ body: bodyJson,signal: headerTimeout.signal,});log.debug("received codex response headers", {diff --git a/src/providers/codex/index.ts b/src/providers/codex/index.tsindex 952719e..42c3bf2 100644--- a/src/providers/codex/index.ts+++ b/src/providers/codex/index.ts@@ -14,6 +14,7 @@ import { accumulateResponse, UpstreamStreamError } from "./translate/accumulate.import { mapUsageToAnthropic } from "./translate/reducer.ts";import { CodexError, postCodex } from "./client.ts";import { countTokens, countTranslatedTokens } from "./count-tokens.ts";+import { summarizeCodexRequestSize } from "./request-summary.ts";import { runBrowserLogin } from "./auth/pkce.ts";import { runDeviceLogin } from "./auth/device.ts";import { persistInitialTokens } from "./auth/manager.ts";@@ -54,7 +55,9 @@ function sessionState(sessionId?: string): SessionTimelineState | undefined {return state;}-function readToolSchemaSummary(tools: { name: string; parameters: unknown; strict?: boolean }[] | undefined) {+function readToolSchemaSummary(+ tools: { name: string; parameters: unknown; strict?: boolean }[] | undefined,+) {const read = tools?.find((tool) => tool.name === "Read");if (!read) return undefined;const parameters = read.parameters;@@ -218,6 +221,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): Promif (err instanceof InvalidServiceTierError) return invalidServiceTierResponse(err);throw err;}+ const requestSize = summarizeCodexRequestSize(translated);const localInputTokens = logVerbose() ? countTokens(body) : undefined;const translatedInputTokens = logVerbose() ? countTranslatedTokens(translated) : undefined;if (state) {@@ -240,6 +244,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): PromrequestedMaxTokens: body.max_tokens,hasContextManagement: contextManagement !== undefined,promptCacheKey: translated.prompt_cache_key,+ requestSize,});if (logVerbose()) log.debug("translated request body", { body: translated });if (logVerbose()) {@@ -258,6 +263,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): PromrequestedMaxTokens: body.max_tokens,hasContextManagement: contextManagement !== undefined,contextManagement,+ requestSize,previousCountReqId: state?.lastCount?.reqId,previousCountModel: state?.lastCount?.model,previousCountTokens: state?.lastCount?.tokens,@@ -298,6 +304,7 @@ async function handleMessages(body: AnthropicRequest, ctx: RequestContext): PromreqId: ctx.reqId,signal: ctx.signal,upstreamHeaders: upstream.headers,+ requestSize,onFinish: logVerbose()? (finish) => {const mappedUsage = finish.usage ? mapUsageToAnthropic(finish.usage) : undefined;diff --git a/src/providers/codex/request-summary.ts b/src/providers/codex/request-summary.tsnew file mode 100644index 0000000..960fdb3--- /dev/null+++ b/src/providers/codex/request-summary.ts@@ -0,0 +1,85 @@+import type { ResponsesInputItem, ResponsesRequest } from "./translate/request.ts";++const encoder = new TextEncoder();++export interface CodexRequestSizeSummary {+ bodyJsonBytes: number;+ instructionsBytes: number;+ inputJsonBytes: number;+ toolsJsonBytes: number;+ textJsonBytes: number;+ reasoningJsonBytes: number;+ includeJsonBytes: number;+ clientMetadataJsonBytes: number;+ inputItemCount: number;+ toolCount: number;+ inputTypeCounts: Record<string, number>;+ roleCounts: Record<string, number>;+ largestInputItems: Array<{ index: number; type: string; role?: string; jsonBytes: number }>;+ largestTools: Array<{ index: number; name: string; jsonBytes: number }>;+}++export function summarizeCodexRequestSize(+ body: ResponsesRequest,+ bodyJson = JSON.stringify(body),+): CodexRequestSizeSummary {+ return {+ bodyJsonBytes: byteLength(bodyJson),+ instructionsBytes: stringBytes(body.instructions),+ inputJsonBytes: jsonBytes(body.input),+ toolsJsonBytes: jsonBytes(body.tools),+ textJsonBytes: jsonBytes(body.text),+ reasoningJsonBytes: jsonBytes(body.reasoning),+ includeJsonBytes: jsonBytes(body.include),+ clientMetadataJsonBytes: jsonBytes(body.client_metadata),+ inputItemCount: body.input.length,+ toolCount: body.tools?.length ?? 0,+ inputTypeCounts: countBy(body.input, (item) => item.type),+ roleCounts: countBy(body.input, (item) => ("role" in item ? item.role : undefined)),+ largestInputItems: largestInputItems(body.input),+ largestTools: largestTools(body.tools),+ };+}++function largestInputItems(input: ResponsesInputItem[]) {+ return input+ .map((item, index) => ({+ index,+ type: item.type,+ role: "role" in item ? item.role : undefined,+ jsonBytes: jsonBytes(item),+ }))+ .sort((a, b) => b.jsonBytes - a.jsonBytes)+ .slice(0, 5);+}++function largestTools(tools: ResponsesRequest["tools"]) {+ return (tools ?? [])+ .map((tool, index) => ({ index, name: tool.name, jsonBytes: jsonBytes(tool) }))+ .sort((a, b) => b.jsonBytes - a.jsonBytes)+ .slice(0, 5);+}++function countBy<T>(items: T[], keyFor: (item: T) => string | undefined): Record<string, number> {+ const counts: Record<string, number> = {};+ for (const item of items) {+ const key = keyFor(item);+ if (!key) continue;+ counts[key] = (counts[key] ?? 0) + 1;+ }+ return counts;+}++function jsonBytes(value: unknown): number {+ if (value === undefined) return 0;+ return byteLength(JSON.stringify(value));+}++function stringBytes(value: string | undefined): number {+ if (!value) return 0;+ return byteLength(value);+}++function byteLength(value: string): number {+ return encoder.encode(value).byteLength;+}diff --git a/src/providers/codex/translate/stream.ts b/src/providers/codex/translate/stream.tsindex b7f80b0..c642af4 100644--- a/src/providers/codex/translate/stream.ts+++ b/src/providers/codex/translate/stream.ts@@ -3,6 +3,7 @@ import { join } from "node:path";import { stateDir } from "../../../paths.ts";import { encodeSseEvent } from "../../../sse.ts";import type { Logger } from "../../../log.ts";+import type { CodexRequestSizeSummary } from "../request-summary.ts";import {createUpstreamStreamDiagnostics,mapUsageToAnthropic,@@ -30,6 +31,7 @@ export function translateStream(reqId?: string;signal?: AbortSignal;upstreamHeaders?: Headers;+ requestSize?: CodexRequestSizeSummary;onFinish?: (finish: {stopReason: "end_turn" | "tool_use" | "max_tokens";usage?: Parameters<typeof mapUsageToAnthropic>[0];@@ -199,7 +201,10 @@ export function translateStream(block.type === "tool" && typeof block.id === "string" && typeof block.name === "string",);const activeToolNames = activeToolCalls.map((tool) => tool.name);- const openBlockDetails = Array.from(openBlocks.entries(), ([index, block]) => ({ index, ...block }));+ const openBlockDetails = Array.from(openBlocks.entries(), ([index, block]) => ({+ index,+ ...block,+ }));if (opts.signal?.aborted) {opts.log.info("stream cancelled", {err: describeError(err),@@ -220,8 +225,13 @@ export function translateStream(clientAborted: opts.signal?.aborted ?? false,diagnostics: describeDiagnostics(diagnostics),upstreamHeaders: describeHeaders(opts.upstreamHeaders),+ requestSize: opts.requestSize,};- const diagnosticFile = await writeDiagnosticFile(opts.reqId, "upstream-stream-error", detail);+ const diagnosticFile = await writeDiagnosticFile(+ opts.reqId,+ "upstream-stream-error",+ detail,+ );opts.log.warn("upstream stream error", { ...detail, diagnosticFile });ensureMessageStart();closeOpenBlocks();@@ -241,8 +251,13 @@ export function translateStream(clientAborted: opts.signal?.aborted ?? false,diagnostics: describeDiagnostics(diagnostics),upstreamHeaders: describeHeaders(opts.upstreamHeaders),+ requestSize: opts.requestSize,};- const diagnosticFile = await writeDiagnosticFile(opts.reqId, "stream-translation-error", detail);+ const diagnosticFile = await writeDiagnosticFile(+ opts.reqId,+ "stream-translation-error",+ detail,+ );opts.log.error("stream translation error", { ...detail, diagnosticFile });ensureMessageStart();closeOpenBlocks();@@ -270,8 +285,12 @@ function describeDiagnostics(diagnostics: ReturnType<typeof createUpstreamStreamchunkCount: diagnostics.stats.chunkCount,eventCount: diagnostics.stats.eventCount,durationMs: now - diagnostics.stats.startedAt,- msSinceLastChunk: diagnostics.stats.lastChunkAt ? now - diagnostics.stats.lastChunkAt : undefined,- msSinceLastEvent: diagnostics.stats.lastEventAt ? now - diagnostics.stats.lastEventAt : undefined,+ msSinceLastChunk: diagnostics.stats.lastChunkAt+ ? now - diagnostics.stats.lastChunkAt+ : undefined,+ msSinceLastEvent: diagnostics.stats.lastEventAt+ ? now - diagnostics.stats.lastEventAt+ : undefined,lastEventType: diagnostics.lastEventType,sawTerminalEvent: diagnostics.sawTerminalEvent,};@@ -295,8 +314,14 @@ async function writeDiagnosticFile(const dir = join(stateDir(), "diagnostics");await mkdir(dir, { recursive: true });const safeReqId = reqId?.replace(/[^a-zA-Z0-9._-]/g, "_") || "unknown";- const file = join(dir, `${new Date().toISOString().replace(/[:.]/g, "-")}-${safeReqId}-${kind}.json`);- await writeFile(file, JSON.stringify({ t: new Date().toISOString(), kind, reqId, ...detail }, null, 2));+ const file = join(+ dir,+ `${new Date().toISOString().replace(/[:.]/g, "-")}-${safeReqId}-${kind}.json`,+ );+ await writeFile(+ file,+ JSON.stringify({ t: new Date().toISOString(), kind, reqId, ...detail }, null, 2),+ );return file;} catch {return undefined;