fix(claude-to-openai): strip Anthropic billing header from system prompt
Remove dynamic x-anthropic-billing-header lines from Claude system prompts when translating to OpenAI format to keep prompt prefixes stable and improve prompt cache hits. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,6 +2,11 @@ import { register } from "../index.js";
|
||||
import { FORMATS } from "../formats.js";
|
||||
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
||||
|
||||
function stripAnthropicBillingHeader(text) {
|
||||
if (typeof text !== "string") return "";
|
||||
return text.replace(/^x-anthropic-billing-header:[^\n]*(?:\r?\n)?/i, "");
|
||||
}
|
||||
|
||||
// Convert Claude request to OpenAI format
|
||||
export function claudeToOpenAIRequest(model, body, stream) {
|
||||
const result = {
|
||||
@@ -23,8 +28,8 @@ export function claudeToOpenAIRequest(model, body, stream) {
|
||||
// System message
|
||||
if (body.system) {
|
||||
const systemContent = Array.isArray(body.system)
|
||||
? body.system.map(s => s.text || "").join("\n")
|
||||
: body.system;
|
||||
? body.system.map(s => stripAnthropicBillingHeader(s.text || "")).filter(Boolean).join("\n")
|
||||
: stripAnthropicBillingHeader(body.system);
|
||||
|
||||
if (systemContent) {
|
||||
result.messages.push({
|
||||
@@ -229,4 +234,3 @@ function convertToolChoice(choice) {
|
||||
|
||||
// Register
|
||||
register(FORMATS.CLAUDE, FORMATS.OPENAI, claudeToOpenAIRequest, null);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user