Trim comments to the non-obvious why
Test and deploy / test (push) Successful in 28s
Test and deploy / deploy (push) Successful in 31s

Drop narration, history and restated-code comments across the repo,
keep short ones for security boundaries, upstream quirks and ordering
constraints, and record the convention in CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 20:49:03 +00:00
co-authored by Claude Opus 5.5
parent effddd2794
commit 4530cb8dc4
222 changed files with 1173 additions and 5423 deletions
+11 -38
View File
@@ -1,13 +1,6 @@
"""The Anthropic client and the one call every Claude caller here makes.
"""The Anthropic client and `ask_json`: one prompt for bare JSON, one parsed reply.
`rowing` (a photo in, three numbers out) is a call of a general shape: one
prompt asking for bare JSON, one reply to parse. That whole sequence is
`ask_json` below, so a caller is a prompt, a pydantic model and three error
strings, and another caller would get the same handling around the call. Also
here: the lazily built client and the two exception types `main` maps to 502
and 503.
Like the modules that use it, this never imports FastAPI.
The two exception types map to 502 and 503 in `main`. Never imports FastAPI.
"""
import functools
@@ -22,37 +15,21 @@ log = logging.getLogger(__name__)
T = TypeVar("T", bound=BaseModel)
# Which model every call uses. CLAUDE_MODEL comes from .env; changing it means
# recreating this container, which is why reading it once at import is enough.
# Blank means the default.
#
# The ids listed in .env.example are the ones this module has been written for. Another
# id is passed through as it is, treated as a model that thinks (see below);
# if the API does not know it, every call fails with its `failed` message and
# the reason is in this service's log.
# Read once at import; blank means the default. Unknown ids are passed
# through and treated as thinking models.
DEFAULT_MODEL = "claude-sonnet-5-5"
MODEL = os.environ.get("CLAUDE_MODEL", "").strip() or DEFAULT_MODEL
# Models that take no `output_config.effort` and do not think unless asked to.
# Everything newer thinks by default, and that changes two things in the
# request - see _model_options.
# Models that take no effort setting and don't think unless asked.
_NO_EFFORT_PREFIXES = ("claude-haiku-4-5",)
# Thinking is billed against `max_tokens` like the reply is, and the callers'
# ceilings are sized for the reply alone (a few hundred tokens of JSON). On a
# model that thinks, this is added on top so that a posting worth a moment's
# thought cannot use the whole allowance and leave no room for the answer.
# Thinking counts against max_tokens, and callers size theirs for the reply.
THINKING_HEADROOM = 4000
# Every call here is extraction or classification: read something, return a
# small JSON object. "low" is the level meant for that - the model skips
# thinking on most simple requests - and it is set explicitly because the
# default differs between models.
# Set explicitly because the default differs between models.
EFFORT = "low"
# Seconds per attempt. The SDK retries twice on top (429/5xx/connection), so
# the worst case is roughly three of these plus backoff; the Go callers' own
# timeout is set above that so they see this side's error, not their own.
# Per attempt; the SDK retries twice. The Go caller's timeout sits above that.
TIMEOUT_S = 30.0
@@ -149,8 +126,7 @@ def ask_json(
if system is not None:
request["system"] = system
# Outside the try: NotConfigured is not an API failure and must reach
# `main` as itself, to become the 503.
# Outside the try so NotConfigured reaches `main` and becomes the 503.
api = (get_client or client)()
try:
message = api.messages.create(**request)
@@ -161,15 +137,12 @@ def ask_json(
log.warning("Claude API unreachable: %s", e)
raise ReadError(failed) from e
# A refusal is an ordinary 200 whose content is not the answer. Say so in
# the log rather than letting it surface as an "empty" or "unparseable"
# reply, which would point at the prompt instead of at the classifier.
# A refusal is a 200; log it as such rather than as an unparseable reply.
if getattr(message, "stop_reason", None) == "refusal":
log.warning("Claude declined the request: %s", getattr(message, "stop_details", None))
raise ReadError(failed)
# By type, not position: on a model that thinks, the first block can be a
# (possibly empty) thinking block.
# By type: on a thinking model the first block may be a thinking block.
text = next((block.text for block in message.content if block.type == "text"), None)
if text is None:
if getattr(message, "stop_reason", None) == "max_tokens":