spoof User-Agent for Agent Router calls to bypass unauthorized client detection

This commit is contained in:
rarebuffalo
2026-06-15 02:54:16 +05:30
parent b1fa5890e8
commit d132536284
3 changed files with 26 additions and 0 deletions

View File

@@ -76,6 +76,10 @@ async def call_ai(
if settings.ai_api_base:
kwargs["api_base"] = settings.ai_api_base
if "agentrouter.org" in settings.ai_api_base.lower():
kwargs["extra_headers"] = {
"User-Agent": "claude-code/0.2.9",
}
# JSON mode: supported natively by OpenAI and LiteLLM proxied Gemini.
# For providers that don't support it, LiteLLM silently ignores the flag.

View File

@@ -53,6 +53,10 @@ async def call_ai(
if api_base:
kwargs["api_base"] = api_base
if "agentrouter.org" in api_base.lower():
kwargs["extra_headers"] = {
"User-Agent": "claude-code/0.2.9",
}
if json_mode:
kwargs["response_format"] = {"type": "json_object"}

View File

@@ -94,3 +94,21 @@ async def test_analyze_file_passes_api_base():
mock_call_ai_json.assert_called_once()
called_kwargs = mock_call_ai_json.call_args[1]
assert called_kwargs["api_base"] == "https://agentrouter.org/v1"
@pytest.mark.asyncio
async def test_call_ai_injects_agentrouter_headers():
with patch("litellm.acompletion", new_callable=AsyncMock) as mock_acompletion:
mock_acompletion.return_value.choices = [
AsyncMock(message=AsyncMock(content="Mock response"))
]
await call_ai(
prompt="Hello",
api_key="mock_key",
model="openai/deepseek-chat",
api_base="https://agentrouter.org/v1"
)
mock_acompletion.assert_called_once()
called_kwargs = mock_acompletion.call_args[1]
assert called_kwargs["extra_headers"] == {"User-Agent": "claude-code/0.2.9"}