Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

support codex hosted web search

Raine Virta4c29448911a1
diff --git a/README.md b/README.md
index 3450812..1799414 100644
--- a/README.md
+++ b/README.md
@@ -266,6 +266,9 @@ the UI as `◐ medium · /effort`) is forwarded as Codex `reasoning.effort` (`lo
`xhigh`. An explicit `codex.effort` / `CCP_CODEX_EFFORT` override still takes
precedence and can also force `none`.
+Claude Code's hosted `web_search_20250305` tool is translated to Codex's native
+Responses `web_search` tool, including non-empty domain filters.
+
Confirmed working on **Plus**:
- `gpt-5.4`
diff --git a/src/anthropic/schema.ts b/src/anthropic/schema.ts
index cd8a899..b24d8e9 100644
--- a/src/anthropic/schema.ts
+++ b/src/anthropic/schema.ts
@@ -49,12 +49,23 @@ export interface AnthropicMessage {
content: string | AnthropicContentBlock[];
}
-export interface AnthropicTool {
+export interface AnthropicFunctionTool {
+ type?: "function" | "custom";
name: string;
description?: string;
input_schema: unknown;
}
+export interface AnthropicWebSearchTool {
+ type: "web_search_20250305";
+ name: string;
+ allowed_domains?: string[];
+ blocked_domains?: string[];
+ max_uses?: number;
+}
+
+export type AnthropicTool = AnthropicFunctionTool | AnthropicWebSearchTool;
+
export interface AnthropicRequest {
model: string;
messages: AnthropicMessage[];
diff --git a/src/providers/codex/count-tokens.ts b/src/providers/codex/count-tokens.ts
index eab82b1..5002a0d 100644
--- a/src/providers/codex/count-tokens.ts
+++ b/src/providers/codex/count-tokens.ts
@@ -1,10 +1,7 @@
import { encode } from "gpt-tokenizer/model/gpt-4o";
import type { AnthropicRequest } from "../../anthropic/schema.ts";
import type { ResponsesRequest } from "./translate/request.ts";
-import {
- countAnthropicTokens,
- IMAGE_TOKEN_ESTIMATE,
-} from "../shared/count-tokens.ts";
+import { countAnthropicTokens, IMAGE_TOKEN_ESTIMATE } from "../shared/count-tokens.ts";
import { countToolSchemaTokens } from "../shared/tool-schema.ts";
export function countTokens(req: AnthropicRequest): number {
@@ -38,9 +35,9 @@ export function countTranslatedTokens(
total += countToolSchemaTokens(
req.tools,
- (tool) => tool.name,
- (tool) => tool.description,
- (tool) => tool.parameters,
+ (tool) => ("name" in tool ? tool.name : tool.type),
+ (tool) => ("description" in tool ? tool.description : undefined),
+ (tool) => ("parameters" in tool ? tool.parameters : undefined),
);
if (req.text?.format?.type === "json_schema") {
diff --git a/src/providers/codex/index.ts b/src/providers/codex/index.ts
index 408dc49..206fa73 100644
--- a/src/providers/codex/index.ts
+++ b/src/providers/codex/index.ts
@@ -19,7 +19,10 @@ import { summarizeCodexRequestSize, type CodexRequestSizeSummary } from "./reque
import { codexPreviousResponseId, logVerbose } from "../../config.ts";
import { clearContinuation, continuationCandidate, recordContinuation } from "./continuation.ts";
import { codexCli } from "./cli.ts";
-import { mapUpstreamHttpErrorToResponse, mapUpstreamStreamErrorToResponse } from "../shared/upstream-errors.ts";
+import {
+ mapUpstreamHttpErrorToResponse,
+ mapUpstreamStreamErrorToResponse,
+} from "../shared/upstream-errors.ts";
interface SessionCountSnapshot {
reqId: string;
@@ -56,7 +59,7 @@ function sessionState(sessionId?: string): SessionTimelineState | undefined {
}
function readToolSchemaSummary(
- tools: { name: string; parameters: unknown; strict?: boolean }[] | undefined,
+ tools: { type?: string; name?: string; parameters?: unknown; strict?: boolean }[] | undefined,
) {
const read = tools?.find((tool) => tool.name === "Read");
if (!read) return undefined;
diff --git a/src/providers/codex/request-summary.ts b/src/providers/codex/request-summary.ts
index 332961a..e1d97bb 100644
--- a/src/providers/codex/request-summary.ts
+++ b/src/providers/codex/request-summary.ts
@@ -92,7 +92,11 @@ function inputImageParts(input: ResponsesInputItem[]) {
function largestTools(tools: ResponsesRequest["tools"]) {
return (tools ?? [])
- .map((tool, index) => ({ index, name: tool.name, jsonBytes: jsonBytes(tool) }))
+ .map((tool, index) => ({
+ index,
+ name: "name" in tool ? tool.name : tool.type,
+ jsonBytes: jsonBytes(tool),
+ }))
.sort((a, b) => b.jsonBytes - a.jsonBytes)
.slice(0, 5);
}
diff --git a/src/providers/codex/translate/reducer.test.ts b/src/providers/codex/translate/reducer.test.ts
index 2d2f2d7..cda3676 100644
--- a/src/providers/codex/translate/reducer.test.ts
+++ b/src/providers/codex/translate/reducer.test.ts
@@ -1,10 +1,19 @@
import { describe, expect, it } from "bun:test";
import { reduceUpstream } from "./reducer.ts";
-import { sse, silentLog, upstreamFromChunks } from "./test-helpers.ts";
+import {
+ sse,
+ silentLog,
+ upstreamFromChunks,
+ upstreamThatErrorsAfterChunks,
+} from "./test-helpers.ts";
async function events(chunks: string[]) {
+ return collectEvents(upstreamFromChunks(chunks));
+}
+
+async function collectEvents(upstream: ReadableStream<Uint8Array>) {
const out = [];
- for await (const event of reduceUpstream(upstreamFromChunks(chunks), silentLog)) out.push(event);
+ for await (const event of reduceUpstream(upstream, silentLog)) out.push(event);
return out;
}
@@ -75,6 +84,93 @@ describe("reduceUpstream finish metadata", () => {
});
});
+ it("treats hosted web search response events as progress", async () => {
+ const out = await events([
+ sse("response.output_item.added", {
+ output_index: 0,
+ item: { type: "web_search_call", id: "ws_1", status: "in_progress" },
+ }),
+ sse("response.web_search_call.in_progress", { output_index: 0, item_id: "ws_1" }),
+ sse("response.web_search_call.searching", { output_index: 0, item_id: "ws_1" }),
+ sse("response.web_search_call.completed", { output_index: 0, item_id: "ws_1" }),
+ sse("response.output_item.done", {
+ output_index: 0,
+ item: { type: "web_search_call", id: "ws_1", status: "completed" },
+ }),
+ sse("response.output_item.added", {
+ output_index: 1,
+ item: { type: "message", id: "msg_upstream" },
+ }),
+ sse("response.output_text.delta", { output_index: 1, delta: "result text" }),
+ sse("response.output_item.done", {
+ output_index: 1,
+ item: { type: "message", id: "msg_upstream" },
+ }),
+ sse("response.completed", { response: { id: "resp_1", usage: { input_tokens: 3 } } }),
+ ]);
+
+ expect(out.filter((event) => event.kind === "progress").length).toBeGreaterThanOrEqual(4);
+ expect(out.at(-1)).toEqual({
+ kind: "finish",
+ stopReason: "end_turn",
+ terminalType: "response.completed",
+ continuationEligible: true,
+ usage: { input_tokens: 3 },
+ responseId: "resp_1",
+ outputItems: [
+ {
+ type: "message",
+ role: "assistant",
+ content: [{ type: "output_text", text: "result text" }],
+ },
+ ],
+ });
+ });
+
+ it("finishes completed tool calls when the Codex WebSocket closes before a terminal event", async () => {
+ const out = await collectEvents(
+ upstreamThatErrorsAfterChunks(
+ [
+ sse("response.output_item.added", {
+ output_index: 0,
+ item: { type: "function_call", call_id: "call_1", name: "WebSearch" },
+ }),
+ sse("response.function_call_arguments.done", {
+ output_index: 0,
+ arguments: '{"query":"claude-code-proxy github"}',
+ }),
+ sse("response.output_item.done", {
+ output_index: 0,
+ item: {
+ type: "function_call",
+ call_id: "call_1",
+ name: "WebSearch",
+ arguments: '{"query":"claude-code-proxy github"}',
+ },
+ }),
+ ],
+ new Error("Codex WebSocket connection closed"),
+ ),
+ );
+
+ expect(out.at(-1)).toEqual({
+ kind: "finish",
+ stopReason: "tool_use",
+ terminalType: "response.incomplete",
+ continuationEligible: false,
+ usage: undefined,
+ responseId: undefined,
+ outputItems: [
+ {
+ type: "function_call",
+ call_id: "call_1",
+ name: "WebSearch",
+ arguments: '{"query":"claude-code-proxy github"}',
+ },
+ ],
+ });
+ });
+
it("marks response.done as continuation eligible when complete", async () => {
const out = await events([
sse("response.done", {
diff --git a/src/providers/codex/translate/reducer.ts b/src/providers/codex/translate/reducer.ts
index 6e8f45a..0f175cb 100644
--- a/src/providers/codex/translate/reducer.ts
+++ b/src/providers/codex/translate/reducer.ts
@@ -234,6 +234,15 @@ export async function* reduceUpstream(
let continuationEligible = false;
let incomplete = false;
+ function canFinishAfterClosedCompletedToolCall(err: unknown): boolean {
+ return (
+ sawToolUse &&
+ outputItemsByIndex.size > 0 &&
+ blocksByOutputIndex.size === 0 &&
+ isCodexWebSocketCloseError(err)
+ );
+ }
+
function captureOutputItem(outputIndex: number, state: BlockState): void {
if (state.kind === "text") {
if (!state.textAccum) return;
@@ -258,6 +267,18 @@ export async function* reduceUpstream(
try {
next = await events.next();
} catch (err) {
+ if (canFinishAfterClosedCompletedToolCall(err)) {
+ diagnostics.sawTerminalEvent = true;
+ terminalType = "response.incomplete";
+ incomplete = false;
+ continuationEligible = false;
+ log.warn("upstream websocket closed after completed tool call", {
+ err: err instanceof Error ? err.message : String(err),
+ lastEventType: diagnostics.lastEventType,
+ stats: diagnostics.stats,
+ });
+ break;
+ }
throw upstreamReadError(err);
}
if (next.done) break;
@@ -292,6 +313,14 @@ export async function* reduceUpstream(
yield { kind: "progress" };
continue;
}
+ if (
+ t === "response.web_search_call.in_progress" ||
+ t === "response.web_search_call.searching" ||
+ t === "response.web_search_call.completed"
+ ) {
+ yield { kind: "progress" };
+ continue;
+ }
if (t === "response.failed" || t === "response.error" || t === "error") {
const message = p?.response?.error?.message || p?.error?.message || "Upstream error";
throw new UpstreamStreamError("failed", message);
@@ -302,6 +331,10 @@ export async function* reduceUpstream(
const outputIndex: number = p.output_index;
if (!item) continue;
if (item.type === "reasoning") continue;
+ if (item.type === "web_search_call") {
+ yield { kind: "progress" };
+ continue;
+ }
if (item.type === "message") {
const idx = anthropicIndex++;
blocksByOutputIndex.set(outputIndex, { kind: "text", index: idx, textAccum: "" });
@@ -502,6 +535,14 @@ export async function* reduceUpstream(
};
}
+function isCodexWebSocketCloseError(err: unknown): boolean {
+ const message = err instanceof Error ? err.message : String(err);
+ return (
+ message.includes("Codex WebSocket connection closed") ||
+ message.includes("Codex WebSocket closed before terminal event")
+ );
+}
+
function upstreamReadError(err: unknown): Error {
if (err instanceof UpstreamStreamError) return err;
const message = err instanceof Error ? err.message : String(err);
diff --git a/src/providers/codex/translate/request.test.ts b/src/providers/codex/translate/request.test.ts
index f678819..f15317b 100644
--- a/src/providers/codex/translate/request.test.ts
+++ b/src/providers/codex/translate/request.test.ts
@@ -35,6 +35,55 @@ describe("translateRequest", () => {
expect(translated.include).toEqual(["reasoning.encrypted_content"]);
});
+ it("translates Anthropic web search to the Codex hosted web_search tool", () => {
+ const translated = translateRequest({
+ ...baseRequest,
+ tools: [
+ {
+ type: "web_search_20250305",
+ name: "web_search",
+ allowed_domains: [],
+ blocked_domains: [],
+ max_uses: 8,
+ },
+ ],
+ });
+
+ expect(translated.tools).toEqual([
+ {
+ type: "web_search",
+ external_web_access: false,
+ search_content_types: ["text", "image"],
+ },
+ ]);
+ });
+
+ it("preserves non-empty Anthropic web search domain filters", () => {
+ const translated = translateRequest({
+ ...baseRequest,
+ tools: [
+ {
+ type: "web_search_20250305",
+ name: "web_search",
+ allowed_domains: ["github.com"],
+ blocked_domains: ["example.com"],
+ },
+ ],
+ });
+
+ expect(translated.tools).toEqual([
+ {
+ type: "web_search",
+ external_web_access: false,
+ search_content_types: ["text", "image"],
+ filters: {
+ allowed_domains: ["github.com"],
+ blocked_domains: ["example.com"],
+ },
+ },
+ ]);
+ });
+
it("normalizes fast service tier to upstream priority", () => {
loadConfig({ env: { CCP_CODEX_SERVICE_TIER: "fast" }, forceReload: true });
diff --git a/src/providers/codex/translate/request.ts b/src/providers/codex/translate/request.ts
index 4881f26..ad8f02c 100644
--- a/src/providers/codex/translate/request.ts
+++ b/src/providers/codex/translate/request.ts
@@ -91,7 +91,9 @@ export type ResponsesContentPart =
| { type: "output_text"; text: string }
| { type: "input_image"; image_url: string; detail?: "auto" | "low" | "high" };
-export interface ResponsesTool {
+export type ResponsesTool = ResponsesFunctionTool | ResponsesWebSearchTool;
+
+export interface ResponsesFunctionTool {
type: "function";
name: string;
description?: string;
@@ -99,6 +101,16 @@ export interface ResponsesTool {
strict?: boolean;
}
+export interface ResponsesWebSearchTool {
+ type: "web_search";
+ external_web_access: boolean;
+ search_content_types: Array<"text" | "image">;
+ filters?: {
+ allowed_domains?: string[];
+ blocked_domains?: string[];
+ };
+}
+
export interface TranslateOptions {
sessionId?: string;
serviceTier?: ServiceTier;
@@ -271,6 +283,17 @@ function isRecord(value: unknown): value is Record<string, unknown> {
}
function toResponsesTool(tool: AnthropicTool): ResponsesTool {
+ if (tool.type === "web_search_20250305") {
+ const filters: ResponsesWebSearchTool["filters"] = {};
+ if (tool.allowed_domains?.length) filters.allowed_domains = tool.allowed_domains;
+ if (tool.blocked_domains?.length) filters.blocked_domains = tool.blocked_domains;
+ return {
+ type: "web_search",
+ external_web_access: false,
+ search_content_types: ["text", "image"],
+ ...(Object.keys(filters).length ? { filters } : {}),
+ };
+ }
return {
type: "function",
name: tool.name,
diff --git a/src/providers/codex/translate/stream.test.ts b/src/providers/codex/translate/stream.test.ts
index 280d0b2..63fe594 100644
--- a/src/providers/codex/translate/stream.test.ts
+++ b/src/providers/codex/translate/stream.test.ts
@@ -35,7 +35,7 @@ function readChunks(deltas: string[], includeDone = true): string[] {
? [
sse("response.output_item.done", {
output_index: 0,
- item: { type: "function_call", arguments: "{\"file_path\":\"/tmp/a\"}" },
+ item: { type: "function_call", arguments: '{"file_path":"/tmp/a"}' },
}),
]
: []),
@@ -76,7 +76,7 @@ function collectWithTime(chunks: string[]): Promise<string> {
describe("translateStream", () => {
it("emits keepalive pings while Read arguments are buffered", async () => {
const chunks = [
- ...readChunks(["{\"file_path\"", "\":\"/tmp/a\"}"]),
+ ...readChunks(['{"file_path"', '":"/tmp/a"}']),
sse("response.completed", { response: { usage: {} } }),
];
@@ -110,7 +110,7 @@ describe("translateStream", () => {
});
it("emits an error instead of message_stop when upstream ends with an open Read block", async () => {
- const chunks = readChunks(["{\"file_path\"", ':"/tmp/a"'], false);
+ const chunks = readChunks(['{"file_path"', ':"/tmp/a"'], false);
const output = await collectFromChunks(chunks);
@@ -123,7 +123,7 @@ describe("translateStream", () => {
});
it("reports upstream read failures as upstream stream errors", async () => {
- const chunks = readChunks(["{\"file_path\""], false);
+ const chunks = readChunks(['{"file_path"'], false);
const err = new DOMException("The connection was closed.", "AbortError");
const logs: Array<{ msg: string; fields: unknown }> = [];
const log = {
@@ -139,15 +139,56 @@ describe("translateStream", () => {
}),
);
- expect(output.indexOf("event: content_block_stop")).toBeLessThan(output.indexOf("event: error"));
+ expect(output.indexOf("event: content_block_stop")).toBeLessThan(
+ output.indexOf("event: error"),
+ );
expect(output).toContain("event: error");
expect(output).toContain("Upstream stream read failed: The connection was closed.");
expect(output).not.toContain("input_json_delta");
expect(logs.some((entry) => entry.msg === "upstream stream error")).toBe(true);
});
+ it("emits tool_use stop when Codex closes after a completed tool call", async () => {
+ const chunks = [
+ sse("response.output_item.added", {
+ output_index: 0,
+ item: { type: "function_call", call_id: "call_search", name: "WebSearch" },
+ }),
+ sse("response.function_call_arguments.done", {
+ output_index: 0,
+ arguments: '{"query":"claude-code-proxy github"}',
+ }),
+ sse("response.output_item.done", {
+ output_index: 0,
+ item: {
+ type: "function_call",
+ call_id: "call_search",
+ name: "WebSearch",
+ arguments: '{"query":"claude-code-proxy github"}',
+ },
+ }),
+ ];
+
+ const output = await collect(
+ translateStream(
+ upstreamThatErrorsAfterChunks(chunks, new Error("Codex WebSocket connection closed")),
+ {
+ messageId: "msg_1",
+ model: "gpt-5.5",
+ log: silentLog,
+ },
+ ),
+ );
+
+ expect(output).toContain("event: content_block_start");
+ expect(output).toContain("input_json_delta");
+ expect(output).toContain('"stop_reason":"tool_use"');
+ expect(output).toContain("event: message_stop");
+ expect(output).not.toContain("event: error");
+ });
+
it("fails buffered Read arguments that exceed the safe duration", async () => {
- const chunks = readChunks(["{\"file_path\"", ':"/tmp/a"'], false);
+ const chunks = readChunks(['{"file_path"', ':"/tmp/a"'], false);
const output = await withMockNow(() =>
collectFromChunks(chunks, {
diff --git a/src/providers/cursor/count-tokens.ts b/src/providers/cursor/count-tokens.ts
index a265ba8..94c9cca 100644
--- a/src/providers/cursor/count-tokens.ts
+++ b/src/providers/cursor/count-tokens.ts
@@ -8,8 +8,8 @@ export function countCursorTokens(req: AnthropicRequest): number {
total += countToolSchemaTokens(
req.tools,
(tool) => tool.name,
- (tool) => tool.description,
- (tool) => tool.input_schema,
+ (tool) => ("description" in tool ? tool.description : undefined),
+ (tool) => ("input_schema" in tool ? tool.input_schema : {}),
);
return total;
}
diff --git a/src/providers/cursor/translate/request.ts b/src/providers/cursor/translate/request.ts
index 474ac4a..ec32422 100644
--- a/src/providers/cursor/translate/request.ts
+++ b/src/providers/cursor/translate/request.ts
@@ -30,8 +30,8 @@ export function renderCursorPrompt(req: AnthropicRequest): string {
.map((tool) =>
JSON.stringify({
name: tool.name,
- description: tool.description,
- input_schema: tool.input_schema,
+ description: "description" in tool ? tool.description : undefined,
+ input_schema: "input_schema" in tool ? tool.input_schema : {},
}),
)
.join("\n")}\n</tools>`,
@@ -90,7 +90,7 @@ function renderBlock(block: AnthropicContentBlock): string {
case "tool_use":
return `<tool_use id="${block.id}" name="${block.name}">\n${JSON.stringify(block.input ?? {})}\n</tool_use>`;
case "tool_result":
- return `<tool_result tool_use_id="${block.tool_use_id}"${block.is_error ? " is_error=\"true\"" : ""}>\n${renderToolResult(block.content)}\n</tool_result>`;
+ return `<tool_result tool_use_id="${block.tool_use_id}"${block.is_error ? ' is_error="true"' : ""}>\n${renderToolResult(block.content)}\n</tool_result>`;
}
}
@@ -100,7 +100,13 @@ function renderToolResult(content: AnthropicToolResultContentBlock[] | string):
}
function renderToolResultBlock(block: AnthropicToolResultContentBlock): string {
- if (block.type === "text" || block.type === "image" || block.type === "tool_use" || block.type === "tool_result" || block.type === "thinking") {
+ if (
+ block.type === "text" ||
+ block.type === "image" ||
+ block.type === "tool_use" ||
+ block.type === "tool_result" ||
+ block.type === "thinking"
+ ) {
return renderBlock(block as AnthropicContentBlock);
}
const type = typeof block.type === "string" ? block.type : "unknown";
@@ -108,17 +114,28 @@ function renderToolResultBlock(block: AnthropicToolResultContentBlock): string {
}
function messageBlocks(message: AnthropicMessage): AnthropicContentBlock[] {
- return typeof message.content === "string" ? [{ type: "text", text: message.content }] : message.content;
+ return typeof message.content === "string"
+ ? [{ type: "text", text: message.content }]
+ : message.content;
}
-function collectImageBlocks(block: AnthropicContentBlock, visit: (block: AnthropicImageBlock) => void): void {
+function collectImageBlocks(
+ block: AnthropicContentBlock,
+ visit: (block: AnthropicImageBlock) => void,
+): void {
if (block.type === "image") {
visit(block);
return;
}
if (block.type !== "tool_result" || typeof block.content === "string") return;
for (const child of block.content) {
- if (child.type === "text" || child.type === "image" || child.type === "tool_use" || child.type === "tool_result" || child.type === "thinking") {
+ if (
+ child.type === "text" ||
+ child.type === "image" ||
+ child.type === "tool_use" ||
+ child.type === "tool_result" ||
+ child.type === "thinking"
+ ) {
collectImageBlocks(child as AnthropicContentBlock, visit);
}
}
diff --git a/src/providers/kimi/translate/request.ts b/src/providers/kimi/translate/request.ts
index 59bbbee..8f46b25 100644
--- a/src/providers/kimi/translate/request.ts
+++ b/src/providers/kimi/translate/request.ts
@@ -221,7 +221,6 @@ function pushAssistantMessage(out: KimiMessage[], blocks: AnthropicContentBlock[
out.push(msg);
}
-
export function toolResultContent(
content: string | AnthropicToolResultContentBlock[],
isError: boolean | undefined,
@@ -249,8 +248,8 @@ function toKimiTool(tool: AnthropicTool): KimiTool {
type: "function",
function: {
name: tool.name,
- description: tool.description,
- parameters: tool.input_schema,
+ description: "description" in tool ? tool.description : undefined,
+ parameters: "input_schema" in tool ? tool.input_schema : {},
},
};
}
diff --git a/src/providers/shared/count-tokens.ts b/src/providers/shared/count-tokens.ts
index 7e05c65..8800004 100644
--- a/src/providers/shared/count-tokens.ts
+++ b/src/providers/shared/count-tokens.ts
@@ -4,7 +4,7 @@ import {
normalizeContent,
toolResultToString,
} from "../translate/anthropic-content.ts";
-import type { AnthropicRequest } from "../../anthropic/schema.ts";
+import type { AnthropicRequest, AnthropicTool } from "../../anthropic/schema.ts";
export const IMAGE_TOKEN_ESTIMATE = 2000;
@@ -32,15 +32,10 @@ export function countAnthropicTokens(
req,
countToken,
tools: req.tools,
- readToolName: (tool: { name: string; description?: string; input_schema: unknown }) =>
- tool.name,
- readToolDescription: (tool: {
- name: string;
- description?: string;
- input_schema: unknown;
- }) => tool.description,
- readToolSchema: (tool: { name: string; description?: string; input_schema: unknown }) =>
- tool.input_schema,
+ readToolName: (tool: AnthropicTool) => tool.name,
+ readToolDescription: (tool: AnthropicTool) =>
+ "description" in tool ? tool.description : undefined,
+ readToolSchema: (tool: AnthropicTool) => ("input_schema" in tool ? tool.input_schema : {}),
};
if (includeThinking) {
return countAnthropicRequestTokensWithSystem({ ...base, includeThinking: true });
@@ -72,17 +67,15 @@ export function countAnthropicRequestTokens<TTool>(
},
): number;
-export function countAnthropicRequestTokens<TTool>(
- {
- req,
- countToken,
- tools,
- readToolName,
- readToolDescription,
- readToolSchema,
- includeThinking = false,
- }: CountAnthropicRequestTokenOptions<TTool> & { includeThinking?: boolean },
-): number {
+export function countAnthropicRequestTokens<TTool>({
+ req,
+ countToken,
+ tools,
+ readToolName,
+ readToolDescription,
+ readToolSchema,
+ includeThinking = false,
+}: CountAnthropicRequestTokenOptions<TTool> & { includeThinking?: boolean }): number {
let total = 0;
for (const msg of req.messages) {
const blocks = normalizeContent(msg.content);
@@ -102,12 +95,7 @@ export function countAnthropicRequestTokens<TTool>(
}
}
- total += countToolSchemaTokens(
- tools,
- readToolName,
- readToolDescription,
- readToolSchema,
- );
+ total += countToolSchemaTokens(tools, readToolName, readToolDescription, readToolSchema);
total += req.messages.length * 4;
return total;