Skip to content

Paid commands stop early without API credentials - #111

Merged
afterimaginary merged 2 commits into
mainfrom
issue-109
Nov 27, 2026
Merged

afterimaginary merged 2 commits into
mainfrom
issue-109

Conversation

@afterimaginary

Copy link
Copy Markdown
Owner

Closes #109

Summary

Without Anthropic credentials, run --model <real model> got past the estimate, wrote estimate.json from character counts, then died with the SDK's raw TypeError ("Could not resolve authentication method") on the first request; estimate --counter api showed the same error as a traceback. Both now stop before doing anything, with one line:

run: error: no Anthropic API credentials found; set an API key in the environment or run `ant auth login`

Changes

  • estimate.has_credentials(client) checks what the SDK found without sending anything: the client's auth headers (from a key or token) or a credentials provider (an ant auth login profile). That mirrors the SDK's own header validation, which is what raises the TypeError.
  • grid.require_credentials() raises the existing RunError, so the commands print the usual <command>: error: … line and exit 1. run checks right after parsing its spec, before the gates, the cases, the run directory or the estimate; fake runs skip it. estimate checks only with --counter api.
  • best_counter uses the same check instead of catching the TypeError, so the keyless fallback from estimate falls back to character counts without credentials #108 keeps its message.
  • The hint names no variable because tests/test_code_rules.py forbids the key variable's name in code; the README already names it.
  • Nothing in gates.HARNESS_FILES changed.

Verification

  • make ready passes.
  • tests/test_run.py and tests/test_estimate.py also pass with credentials hidden (no ANTHROPIC_* variables, empty HOME), as in CI. The three paid-run tests that fake the model now opt past the check with a credentials_found fixture.
  • New tests, using the no_credentials fixture: a paid run stops with only that line, creates nothing in the runs dir and never reaches the gates, cases or counter; a fake run still works keyless; estimate --counter api stops with only that line before loading anything; has_credentials is false with nothing found and true for a token or a credentials provider.
  • Checked by hand with credentials hidden: both commands print the one line and exit 1, and no run directory is created. With the real profile, has_credentials is true.

Observations

  • memories --max-usd also makes paid calls and has no such check. It's outside this issue's two commands.

`run --model <real model>` and `estimate --counter api` now check that the
SDK found credentials before doing any work, and stop with one line saying
none were found and how to provide them, instead of dying on the SDK's bare
TypeError at the first request (after `run` had already written its
estimate). The check reads the client's auth headers and credentials
provider, so nothing is sent. `best_counter` uses the same check in place of
catching the TypeError.

Closes #109

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

Copy link
Copy Markdown
Owner Author

Review (fresh-context reviewer)

Verdict: Correct. No P0–P2 defects, one possible P2 scope gap, two P3 defects, five refinements.
Intent: issue #109

Spec

  • [Missing, P2, judgment call] memories is also a paid command and gets no check. src/backread_exp/memories.py:253. The title says "Paid commands should fail early without API credentials", but the body names only run and estimate --counter api. memories --max-usd N without credentials would still hit the SDK's TypeError on its first request.
  • [Misread, P3, low confidence] "how to provide them" is vague. src/backread_exp/estimate.py:139. The hint says "set an API key in the environment" without naming the variable, because tests/test_code_rules.py bans that name in code. ant auth login works as a fix on its own.

Defects

[P3] Could refuse a setup the SDK would accept. src/backread_exp/estimate.py:144 (correctness, low confidence). If an Authorization or X-Api-Key header comes only from custom default headers, has_credentials returns false even though the SDK would accept the request. Very unlikely here.

[P3] A broken profile shows a traceback. src/backread_exp/grid.py:214 (correctness, low confidence). If ANTHROPIC_PROFILE names a missing or corrupt profile, anthropic.Anthropic() raises while the client is built, and _reporting_errors doesn't catch it. That failure happened before this change too, just later, so the change doesn't make it worse.

Refinements

  • A test guards a branch that's gone. tests/test_estimate.py:187 (maintain). test_best_counter_does_not_hide_other_type_errors tested the narrow except TypeError this PR removed, so it now passes trivially. The neighbouring docstring still describes the old mechanism.
  • The test depends on an argument's position. tests/test_run.py:727 (maintain). args[4] = "claude-haiku-5-5" relies on where --model's value sits in run_args.
  • The module docstring is badly wrapped. src/backread_exp/grid.py:7 (maintain).
  • The check is split across two modules. src/backread_exp/estimate.py:139 (simplify). NO_CREDENTIALS lives in estimate.py, but only grid.py uses it.
  • A client is built for the check and never closed. src/backread_exp/grid.py:214 (efficiency). Use it as a context manager.

Keep the message next to the check in grid.py and close the client built
for it. Drop the test of the removed TypeError handler, update its
neighbour's docstring, spell out the paid run's arguments in the new test,
and rewrap the module docstring.

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

Copy link
Copy Markdown
Owner Author

Review follow-up

Fixed:

  • Removed test_best_counter_does_not_hide_other_type_errors, which tested the except TypeError this PR removed, and updated the neighbouring test's docstring to describe the credentials check.
  • The new paid-run test writes out its arguments instead of setting args[4].
  • Rewrapped grid.py's module docstring.
  • Moved NO_CREDENTIALS into grid.py next to require_credentials, its only user.
  • require_credentials builds its client in a with block, so it's closed.

Declined:

  • memories gets no check: the issue names only run and estimate --counter api. Flagged as a follow-up instead.
  • The hint doesn't name the variable: tests/test_code_rules.py bans the name in code. ant auth login works on its own, and the README names the variable.
  • Auth headers that come only from custom default headers: not a setup this project uses, and the SDK documents keys, tokens and profiles.
  • A traceback for a broken profile: that failure happened before this PR too, just later, so this change doesn't make it worse. Better handled on its own if it ever comes up.

make ready passes, and tests/test_run.py and tests/test_estimate.py pass with credentials hidden.

@afterimaginary
afterimaginary marked this pull request as ready for review November 27, 2026 08:44
@afterimaginary
afterimaginary merged commit 296f953 into main Nov 27, 2026
1 check passed
@afterimaginary
afterimaginary deleted the issue-109 branch November 27, 2026 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Paid commands should fail early without API credentials

1 participant