Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/agentos/provider/failures.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,15 @@ def _is_context_overflow(text: str) -> bool:
"input exceeds",
"provider_request_budget_exhausted",
"too many tokens",
# Gemini's canonical input-token-limit error, e.g. "The input token
# count (5911388) exceeds the maximum number of tokens allowed
# (1048576)." — does not contain "input exceeds" or "maximum
# context" since the token counts sit between the fixed phrases.
"input token count",
"exceeds the maximum number of tokens allowed",
)
)


def _is_policy_refusal(text: str) -> bool:
return any(
marker in text
Expand Down
40 changes: 40 additions & 0 deletions tests/test_provider_failures.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,43 @@ def test_provider_request_budget_exhausted_is_context_overflow() -> None:
)
is ProviderFailureKind.CONTEXT_OVERFLOW
)

def test_gemini_input_token_count_message_is_context_overflow() -> None:
# Gemini's real, canonical context-overflow error. Numbers vary per
# request but the surrounding phrasing is fixed. Before the fix this
# matched none of the context-overflow markers and fell through to
# BAD_REQUEST, so the runtime never triggered COMPACT_AND_RETRY.
message = (
"The input token count (5911388) exceeds the maximum number of "
"tokens allowed (1048576)."
)

assert (
classify_provider_error(
provider_name="gemini",
status_code=400,
raw_code="400",
message=message,
)
is ProviderFailureKind.CONTEXT_OVERFLOW
)


def test_gemini_input_token_count_message_is_context_overflow_regardless_of_token_counts() -> (
None
):
# Same shape, different digit counts — guards against a marker that
# accidentally depends on a specific number of digits.
message = (
"The input token count (132478) exceeds the maximum number of "
"tokens allowed (131072)."
)

assert (
classify_provider_error(
provider_name="gemini",
status_code=400,
message=message,
)
is ProviderFailureKind.CONTEXT_OVERFLOW
)