Skip to content

Commit e3e62c4

Browse files
committed
fix: report an Anthropic token-count failure as an LLM error instead of an SDK traceback
1 parent bbb7bdd commit e3e62c4

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

pr_split/exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ErrorMsg(StrEnum):
1818
MERGE_CONFLICT = "Groups '{a}' and '{b}' modify overlapping regions in '{file}'"
1919
NO_PLAN = "No split plan found; run 'pr-split split' first"
2020
LLM_PARSE_ERROR = "Failed to parse LLM response: {detail}"
21+
LLM_TOKEN_COUNT_FAILED = "Could not count prompt tokens with the LLM API: {detail}"
2122
BRANCH_CREATE_FAILED = "Failed to create branch '{branch}': {detail}"
2223
PR_CREATE_FAILED = "Failed to create PR for group '{group}': {detail}"
2324
MERGE_FAILED = "Merge of '{source}' into '{target}' failed: {detail}"

pr_split/planner/client.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,18 @@ def _extract_raw_output(block_input: dict[str, object]) -> list[_RawGroup]:
9090

9191
def _count_tokens_anthropic(system: str, user: str, *, settings: Settings) -> int:
9292
client = anthropic.Anthropic(api_key=settings.api_key)
93-
response = client.messages.count_tokens(
94-
model=settings.model,
95-
system=system,
96-
messages=[{"role": "user", "content": user}],
97-
tools=[_ANTHROPIC_TOOL_DEF],
98-
)
93+
# This is the first API call of a run, so a bad key, a network blip or a
94+
# rate limit surfaces here; it must be an LLMError like every other
95+
# provider failure rather than an SDK traceback.
96+
try:
97+
response = client.messages.count_tokens(
98+
model=settings.model,
99+
system=system,
100+
messages=[{"role": "user", "content": user}],
101+
tools=[_ANTHROPIC_TOOL_DEF],
102+
)
103+
except anthropic.APIError as exc:
104+
raise LLMError(ErrorMsg.LLM_TOKEN_COUNT_FAILED(detail=str(exc))) from exc
99105
return response.input_tokens
100106

101107

tests/test_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from types import SimpleNamespace
55
from unittest.mock import MagicMock, patch
66

7+
import anthropic
78
import pytest
89

910
from pr_split.config import Settings
@@ -547,6 +548,16 @@ def test_returns_input_tokens(self, mock_cls: MagicMock) -> None:
547548
assert result == 42
548549
mock_client.messages.count_tokens.assert_called_once()
549550

551+
@patch("pr_split.planner.client.anthropic.Anthropic")
552+
def test_api_failure_is_an_llm_error(self, mock_cls: MagicMock) -> None:
553+
mock_client = mock_cls.return_value
554+
mock_client.messages.count_tokens.side_effect = anthropic.APIConnectionError(
555+
request=MagicMock()
556+
)
557+
settings = _make_settings(Provider.ANTHROPIC)
558+
with pytest.raises(LLMError, match="Could not count prompt tokens"):
559+
_count_tokens_anthropic("sys", "usr", settings=settings)
560+
550561

551562
# ---------------------------------------------------------------------------
552563
# _count_tokens_openai

0 commit comments

Comments
 (0)