Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
preserve output budget and log usage drift
Raine Virta0e540551dd3c
diff --git a/src/server.ts b/src/server.tsindex c0312ec..b0ff6f6 100644--- a/src/server.ts+++ b/src/server.ts@@ -4,6 +4,7 @@ import { assertAllowedModel, ModelNotAllowedError, resolveModel } from "./translimport { translateRequest } from "./translate/request.ts"import { translateStream } from "./translate/stream.ts"import { accumulateResponse, UpstreamStreamError } from "./translate/accumulate.ts"+import { mapUsageToAnthropic } from "./translate/reducer.ts"import { CodexError, postCodex } from "./codex/client.ts"import { countTokens, countTranslatedTokens } from "./count-tokens.ts"@@ -31,6 +32,20 @@ interface SessionMessageSnapshot {translatedInputTokens?: number}+function usageWindowTokens(usage: {+ input_tokens: number+ output_tokens: number+ cache_creation_input_tokens: number+ cache_read_input_tokens: number+}): number {+ return (+ usage.input_tokens ++ usage.output_tokens ++ usage.cache_creation_input_tokens ++ usage.cache_read_input_tokens+ )+}+interface SessionTimelineState {seq: numberlastCount?: SessionCountSnapshot@@ -160,6 +175,8 @@ async function handleMessages(req: Request, reqId: string): Promise<Response> {toolCount,stream: wantStream,sessionId,+ requestedMaxTokens: body.max_tokens,+ hasContextManagement: body.context_management !== undefined,hasJsonSchemaFormat: body.output_config?.format?.type === "json_schema",})if (VERBOSE) log.debug("anthropic request body", { reqId, body })@@ -199,6 +216,9 @@ async function handleMessages(req: Request, reqId: string): Promise<Response> {inputItems: translated.input.length,tools: translated.tools?.length ?? 0,hasInstructions: !!translated.instructions,+ requestedMaxTokens: body.max_tokens,+ translatedMaxOutputTokens: translated.max_output_tokens,+ hasContextManagement: body.context_management !== undefined,promptCacheKey: translated.prompt_cache_key,})if (VERBOSE) log.debug("translated request body", { reqId, body: translated })@@ -217,6 +237,9 @@ async function handleMessages(req: Request, reqId: string): Promise<Response> {inputItems: translated.input.length,translatedToolCount: translated.tools?.length ?? 0,hasInstructions: !!translated.instructions,+ requestedMaxTokens: body.max_tokens,+ translatedMaxOutputTokens: translated.max_output_tokens,+ hasContextManagement: body.context_management !== undefined,previousCountReqId: state?.lastCount?.reqId,previousCountModel: state?.lastCount?.model,previousCountTokens: state?.lastCount?.tokens,@@ -257,6 +280,7 @@ async function handleMessages(req: Request, reqId: string): Promise<Response> {sessionId,onFinish: VERBOSE? (finish) => {+ const mappedUsage = finish.usage ? mapUsageToAnthropic(finish.usage) : undefinedlog.info("compaction telemetry", {reqId,phase: "upstream_finish",@@ -269,9 +293,18 @@ async function handleMessages(req: Request, reqId: string): Promise<Response> {toolCount,localInputTokens,translatedInputTokens,+ requestedMaxTokens: body.max_tokens,+ translatedMaxOutputTokens: translated.max_output_tokens,+ hasContextManagement: body.context_management !== undefined,upstreamInputTokens: finish.usage?.input_tokens ?? 0,upstreamOutputTokens: finish.usage?.output_tokens ?? 0,upstreamCachedInputTokens: finish.usage?.input_tokens_details?.cached_tokens ?? 0,+ upstreamReasoningTokens:+ finish.usage?.output_tokens_details?.reasoning_tokens ?? 0,+ mappedInputTokens: mappedUsage?.input_tokens ?? 0,+ mappedOutputTokens: mappedUsage?.output_tokens ?? 0,+ mappedCachedInputTokens: mappedUsage?.cache_read_input_tokens ?? 0,+ mappedContextWindowTokens: mappedUsage ? usageWindowTokens(mappedUsage) : 0,stopReason: finish.stopReason,})}@@ -302,13 +335,21 @@ async function handleMessages(req: Request, reqId: string): Promise<Response> {toolCount,localInputTokens,translatedInputTokens,- upstreamInputTokens: result.usage.input_tokens,- upstreamOutputTokens: result.usage.output_tokens,- upstreamCachedInputTokens: result.usage.cache_read_input_tokens,- stopReason: result.stop_reason,+ requestedMaxTokens: body.max_tokens,+ translatedMaxOutputTokens: translated.max_output_tokens,+ hasContextManagement: body.context_management !== undefined,+ upstreamInputTokens: result.rawUsage?.input_tokens ?? 0,+ upstreamOutputTokens: result.rawUsage?.output_tokens ?? 0,+ upstreamCachedInputTokens: result.rawUsage?.input_tokens_details?.cached_tokens ?? 0,+ upstreamReasoningTokens: result.rawUsage?.output_tokens_details?.reasoning_tokens ?? 0,+ mappedInputTokens: result.response.usage.input_tokens,+ mappedOutputTokens: result.response.usage.output_tokens,+ mappedCachedInputTokens: result.response.usage.cache_read_input_tokens,+ mappedContextWindowTokens: usageWindowTokens(result.response.usage),+ stopReason: result.response.stop_reason,})}- return new Response(JSON.stringify(result), {+ return new Response(JSON.stringify(result.response), {headers: { "content-type": "application/json" },})} catch (err) {diff --git a/src/translate/accumulate.ts b/src/translate/accumulate.tsindex 9e1ae95..14fb047 100644--- a/src/translate/accumulate.ts+++ b/src/translate/accumulate.ts@@ -1,4 +1,5 @@import { mapUsageToAnthropic, reduceUpstream } from "./reducer.ts"+import type { CodexUsage } from "./reducer.ts"export { UpstreamStreamError } from "./reducer.ts"@@ -21,6 +22,11 @@ export interface AnthropicNonStreamResponse {}}+export interface AccumulatedResponse {+ response: AnthropicNonStreamResponse+ rawUsage?: CodexUsage+}+/*** Drive the Codex SSE stream to completion through the shared reducer* and fold the ReducerEvents into a single Anthropic non-streaming@@ -30,7 +36,7 @@ export interface AnthropicNonStreamResponse {export async function accumulateResponse(upstream: ReadableStream<Uint8Array>,opts: { messageId: string; model: string },-): Promise<AnthropicNonStreamResponse> {+): Promise<AccumulatedResponse> {type Block =| { kind: "text"; text: string }| { kind: "tool"; id: string; name: string; args: string }@@ -39,6 +45,7 @@ export async function accumulateResponse(const blocks = new Map<number, Block>()let stopReason: AnthropicNonStreamResponse["stop_reason"] = nulllet usage: ReturnType<typeof mapUsageToAnthropic> | undefined+ let rawUsage: CodexUsage | undefinedfor await (const e of reduceUpstream(upstream)) {switch (e.kind) {@@ -65,6 +72,7 @@ export async function accumulateResponse(breakcase "finish":stopReason = e.stopReason+ rawUsage = e.usageusage = mapUsageToAnthropic(e.usage)break}@@ -88,18 +96,21 @@ export async function accumulateResponse(}return {- id: opts.messageId,- type: "message",- role: "assistant",- model: opts.model,- content,- stop_reason: stopReason,- stop_sequence: null,- usage: usage ?? {- input_tokens: 0,- output_tokens: 0,- cache_creation_input_tokens: 0,- cache_read_input_tokens: 0,+ rawUsage,+ response: {+ id: opts.messageId,+ type: "message",+ role: "assistant",+ model: opts.model,+ content,+ stop_reason: stopReason,+ stop_sequence: null,+ usage: usage ?? {+ input_tokens: 0,+ output_tokens: 0,+ cache_creation_input_tokens: 0,+ cache_read_input_tokens: 0,+ },},}}diff --git a/src/translate/request.ts b/src/translate/request.tsindex ec5a393..8d2b50f 100644--- a/src/translate/request.ts+++ b/src/translate/request.ts@@ -12,6 +12,7 @@ export interface ResponsesRequest {instructions?: stringinput: ResponsesInputItem[]tools?: ResponsesTool[]+ max_output_tokens?: numbertool_choice?:| "auto"| "none"@@ -95,6 +96,7 @@ export function translateRequest(req: AnthropicRequest, opts: TranslateOptions =}if (instructions) out.instructions = instructionsif (tools && tools.length) out.tools = tools+ if (req.max_tokens) out.max_output_tokens = req.max_tokensif (opts.sessionId) out.prompt_cache_key = opts.sessionIdconst effort = req.output_config?.effortif (effort) out.reasoning = { effort }