Add User-Agent header for Anthropic API calls (#5174)

* Add User-Agent header for Anthropic API calls

Passes User-Agent: AnythingLLM/{version} to the Anthropic SDK
so Anthropic can identify traffic from AnythingLLM.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* remove test, simplify header default

* unset change to spread

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Timothy Carambat <rambat1010@gmail.com>
This commit is contained in:
Mike Lambert 2026-03-23 18:19:33 -04:00 committed by GitHub
parent 192ca411f2
commit 9d242bc053
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 0 deletions

View File

@ -9,6 +9,7 @@ const { MODEL_MAP } = require("../modelMap");
const { const {
LLMPerformanceMonitor, LLMPerformanceMonitor,
} = require("../../helpers/chat/LLMPerformanceMonitor"); } = require("../../helpers/chat/LLMPerformanceMonitor");
const { getAnythingLLMUserAgent } = require("../../../endpoints/utils");
class AnthropicLLM { class AnthropicLLM {
constructor(embedder = null, modelPreference = null) { constructor(embedder = null, modelPreference = null) {
@ -20,6 +21,9 @@ class AnthropicLLM {
const AnthropicAI = require("@anthropic-ai/sdk"); const AnthropicAI = require("@anthropic-ai/sdk");
const anthropic = new AnthropicAI({ const anthropic = new AnthropicAI({
apiKey: process.env.ANTHROPIC_API_KEY, apiKey: process.env.ANTHROPIC_API_KEY,
defaultHeaders: {
"User-Agent": getAnythingLLMUserAgent(),
},
}); });
this.anthropic = anthropic; this.anthropic = anthropic;
this.model = this.model =

View File

@ -3,6 +3,7 @@ const { RetryError } = require("../error.js");
const Provider = require("./ai-provider.js"); const Provider = require("./ai-provider.js");
const { v4 } = require("uuid"); const { v4 } = require("uuid");
const { safeJsonParse } = require("../../../http"); const { safeJsonParse } = require("../../../http");
const { getAnythingLLMUserAgent } = require("../../../../endpoints/utils");
/** /**
* The agent provider for the Anthropic API. * The agent provider for the Anthropic API.
@ -16,6 +17,9 @@ class AnthropicProvider extends Provider {
options = { options = {
apiKey: process.env.ANTHROPIC_API_KEY, apiKey: process.env.ANTHROPIC_API_KEY,
maxRetries: 3, maxRetries: 3,
defaultHeaders: {
"User-Agent": getAnythingLLMUserAgent(),
},
}, },
model = "claude-3-5-sonnet-20240620", model = "claude-3-5-sonnet-20240620",
} = config; } = config;