Skip to content

fix(debug): show correct URL and API key in request dumps for all api_modes - #54221

Open
liuhao1024 wants to merge 3 commits into
NousResearch:mainfrom
liuhao1024:liuhao/cron-bugfix-54206-oneshot-dump-anthropic-url
Open

fix(debug): show correct URL and API key in request dumps for all api_modes#54221
liuhao1024 wants to merge 3 commits into
NousResearch:mainfrom
liuhao1024:liuhao/cron-bugfix-54206-oneshot-dump-anthropic-url

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes the dump_api_request_debug function so that request dumps show the correct URL and API key for all api_mode values (anthropic_messages, bedrock_converse, codex_responses, chat_completions).

Previously, the dump function had two bugs:

  1. URL: Only codex_responses got /responses; everything else got /chat/completions — even anthropic_messages (which actually hits /v1/messages) and bedrock_converse (/converse).

  2. API key: Always read from agent.client.api_key. In anthropic_messages mode agent.client is None (the Anthropic SDK client lives at agent._anthropic_client), so the dump showed Bearer None even when a valid key was present.

This made debugging provider-side errors extremely confusing — issue #54206 reports a hermes -z user seeing POST https://api.anthropic.com/chat/completions with Bearer None in the dump and concluding the request was misrouted, when in fact the Anthropic SDK was sending to /v1/messages with the correct key.

Related Issue

Fixes #54206

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • agent/agent_runtime_helpers.py: Dispatch URL construction on api_mode (/v1/messages for anthropic, /converse for bedrock, /responses for codex, /chat/completions default). Fall back to agent._anthropic_api_key or agent.api_key when agent.client is absent. Add api_mode field to dump payload.
  • tests/run_agent/test_run_agent_codex_responses.py: Add test_dump_api_request_debug_uses_anthropic_messages_url (verifies URL and key for anthropic_messages mode) and test_dump_api_request_debug_includes_api_mode (verifies api_mode field in dump payload).

How to Test

  1. python -m pytest tests/run_agent/test_run_agent_codex_responses.py::test_dump_api_request_debug_uses_anthropic_messages_url tests/run_agent/test_run_agent_codex_responses.py::test_dump_api_request_debug_includes_api_mode tests/run_agent/test_run_agent_codex_responses.py::test_dump_api_request_debug_uses_responses_url tests/run_agent/test_run_agent_codex_responses.py::test_dump_api_request_debug_uses_chat_completions_url tests/run_agent/test_run_agent_codex_responses.py::test_dump_api_request_debug_redacts_request_and_error_secrets -q — should pass
  2. Observe that the Anthropic test verifies url == "https://api.anthropic.com/v1/messages" and Authorization does not contain "None".

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

…_modes

The dump_api_request_debug function had two bugs when api_mode was
anthropic_messages or bedrock_converse:

1. URL construction only handled codex_responses (/responses) vs the
   default (/chat/completions).  anthropic_messages requests actually
   go to /v1/messages, and bedrock_converse to /converse, but the dump
   always showed /chat/completions for non-codex modes.

2. API key extraction read from agent.client.api_key, but in
   anthropic_messages mode agent.client is None (the Anthropic SDK
   client lives at agent._anthropic_client).  This caused the dump to
   show "Bearer None" even when a valid key was present.

Fix both by dispatching on api_mode for URL construction and falling
back to agent._anthropic_api_key (Anthropic) or a literal "aws-sdk"
(Bedrock) when agent.client is absent.  Also include api_mode in the
dump payload so the active transport is visible without guessing.

Fixes NousResearch#54206
@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have labels Jun 28, 2026

@tonydwb tonydwb left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes debug dump accuracy across all transport modes (85 additions). Correct URL and API key resolution for anthropic_messages, bedrock_converse, and codex_responses modes. Includes tests for anthropic and chat_completions modes. Good observability improvement.

Reviewed by Hermes Agent

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for isolating the misleading Anthropic dump; the current-head helper still hardcodes non-Codex modes to /chat/completions and reads only agent.client.api_key (agent/agent_runtime_helpers.py:1443-1457), while Anthropic initialization deliberately sets agent.client = None (agent/agent_init.py:817-831).

Problems

  • The Bedrock branch in this diff reports Bearer aws-sdk and <base>/converse. Current Bedrock execution uses boto3's AWS credential chain (agent/bedrock_adapter.py:91-101) and calls client.converse(**kwargs) with modelId (agent/chat_completion_helpers.py:274-278; agent/bedrock_adapter.py:944-950), so those fields do not faithfully describe the transport.
  • The new tests cover Anthropic and default chat-completions only; no test exercises the changed Bedrock branch.

Suggested changes

  • Label Bedrock authentication as AWS SDK/SigV4 rather than a Bearer key, and report an accurately derived request target or an explicitly labeled SDK operation.
  • Add a Bedrock regression test for that representation.

Automated hermes-sweeper review.

if agent.api_mode == "anthropic_messages":
api_key = getattr(agent, "_anthropic_api_key", None)
elif agent.api_mode == "bedrock_converse":
api_key = "aws-sdk"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bedrock authenticates through boto3's AWS credential chain (agent/bedrock_adapter.py:91-101), not a bearer API key. Please record an explicitly labeled AWS SDK/SigV4 auth mode instead of fabricating Bearer aws-sdk in a request dump.

elif agent.api_mode == "anthropic_messages":
_url = f"{_base}/v1/messages"
elif agent.api_mode == "bedrock_converse":
_url = f"{_base}/converse"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The live Bedrock path invokes boto3 client.converse(**kwargs) with a modelId (agent/chat_completion_helpers.py:274-278, agent/bedrock_adapter.py:944-950); <base>/converse is not enough to claim it is the actual outbound request URL. Please emit a labeled SDK operation or derive the complete target.

assert payload["request"]["url"] == "http://127.0.0.1:9208/v1/chat/completions"


def test_dump_api_request_debug_uses_anthropic_messages_url(monkeypatch, tmp_path):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a companion Bedrock-mode regression test. This PR changes the Bedrock URL/auth branch, but this test only exercises the Anthropic branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have sweeper:blast-contained Sweeper blast radius: contained — one narrow path / opt-in / few users type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic api_mode + credential lost on oneshot (-z) AND interactive --resume: request sent as chat_completions + Bearer None

4 participants