Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

Raise stream and upstream retry attempts to five total.

Alishahryar1e79b430cc27b
diff --git a/core/anthropic/stream_recovery.py b/core/anthropic/stream_recovery.py
index 57587778..54d7c088 100644
--- a/core/anthropic/stream_recovery.py
+++ b/core/anthropic/stream_recovery.py
@@ -14,8 +14,9 @@ import jsonschema
import openai
from loguru import logger
-EARLY_TRANSPARENT_RETRIES = 3
-MIDSTREAM_RECOVERY_ATTEMPTS = 2
+EARLY_TRANSPARENT_TOTAL_ATTEMPTS = 5
+EARLY_TRANSPARENT_MAX_RETRIES = EARLY_TRANSPARENT_TOTAL_ATTEMPTS - 1
+MIDSTREAM_RECOVERY_ATTEMPTS = 5
EARLY_HOLDBACK_SECONDS = 0.75
RECOVERY_BUFFER_MAX_BYTES = 65_536
diff --git a/providers/anthropic_messages.py b/providers/anthropic_messages.py
index 13ffddf7..59aaef0c 100644
--- a/providers/anthropic_messages.py
+++ b/providers/anthropic_messages.py
@@ -25,7 +25,8 @@ from core.anthropic.native_sse_block_policy import (
)
from core.anthropic.stream_contracts import parse_sse_text
from core.anthropic.stream_recovery import (
- EARLY_TRANSPARENT_RETRIES,
+ EARLY_TRANSPARENT_MAX_RETRIES,
+ EARLY_TRANSPARENT_TOTAL_ATTEMPTS,
MIDSTREAM_RECOVERY_ATTEMPTS,
RecoveryHoldbackBuffer,
TruncatedProviderStreamError,
@@ -623,7 +624,7 @@ class AnthropicMessagesTransport(BaseProvider):
and stream_opened
and is_retryable_stream_error(error)
and not complete_tool_salvageable
- and early_retries < EARLY_TRANSPARENT_RETRIES
+ and early_retries < EARLY_TRANSPARENT_MAX_RETRIES
):
early_retries += 1
holdback.discard()
@@ -643,7 +644,7 @@ class AnthropicMessagesTransport(BaseProvider):
provider=self._provider_name,
request_id=request_id,
attempt=early_retries,
- max_attempts=EARLY_TRANSPARENT_RETRIES,
+ max_attempts=EARLY_TRANSPARENT_TOTAL_ATTEMPTS,
exc_type=type(error).__name__,
)
continue
diff --git a/providers/openai_compat.py b/providers/openai_compat.py
index 9c61f6af..258262e6 100644
--- a/providers/openai_compat.py
+++ b/providers/openai_compat.py
@@ -23,7 +23,8 @@ from core.anthropic import (
map_stop_reason,
)
from core.anthropic.stream_recovery import (
- EARLY_TRANSPARENT_RETRIES,
+ EARLY_TRANSPARENT_MAX_RETRIES,
+ EARLY_TRANSPARENT_TOTAL_ATTEMPTS,
MIDSTREAM_RECOVERY_ATTEMPTS,
RecoveryHoldbackBuffer,
TruncatedProviderStreamError,
@@ -794,7 +795,7 @@ class OpenAIChatTransport(BaseProvider):
and stream_opened
and is_retryable_stream_error(e)
and not complete_tool_salvageable
- and early_retries < EARLY_TRANSPARENT_RETRIES
+ and early_retries < EARLY_TRANSPARENT_MAX_RETRIES
):
early_retries += 1
holdback.discard()
@@ -813,7 +814,7 @@ class OpenAIChatTransport(BaseProvider):
provider=tag,
request_id=request_id,
attempt=early_retries,
- max_attempts=EARLY_TRANSPARENT_RETRIES,
+ max_attempts=EARLY_TRANSPARENT_TOTAL_ATTEMPTS,
exc_type=type(e).__name__,
)
continue
diff --git a/providers/rate_limit.py b/providers/rate_limit.py
index 200e8833..ce85b674 100644
--- a/providers/rate_limit.py
+++ b/providers/rate_limit.py
@@ -16,6 +16,9 @@ from core.trace import trace_event
T = TypeVar("T")
+UPSTREAM_TRANSIENT_TOTAL_ATTEMPTS = 5
+DEFAULT_UPSTREAM_MAX_RETRIES = UPSTREAM_TRANSIENT_TOTAL_ATTEMPTS - 1
+
def _upstream_http_retryable(code: int) -> bool:
"""True for rate limit / upstream server failures that should backoff-retry."""
@@ -224,7 +227,7 @@ class GlobalRateLimiter:
self,
fn: Callable[..., Any],
*args: Any,
- max_retries: int = 3,
+ max_retries: int = DEFAULT_UPSTREAM_MAX_RETRIES,
base_delay: float = 2.0,
max_delay: float = 60.0,
jitter: float = 1.0,
diff --git a/pyproject.toml b/pyproject.toml
index 8ece905a..cde57a8f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "free-claude-code"
-version = "1.2.39"
+version = "1.2.40"
description = "Middleware between Claude Code CLI (Anthropic API) and NVIDIA NIM"
readme = "README.md"
requires-python = ">=3.14.0"
diff --git a/tests/core/anthropic/test_stream_recovery.py b/tests/core/anthropic/test_stream_recovery.py
index 32c7342a..b1f940f0 100644
--- a/tests/core/anthropic/test_stream_recovery.py
+++ b/tests/core/anthropic/test_stream_recovery.py
@@ -3,6 +3,9 @@
import httpx
from core.anthropic.stream_recovery import (
+ EARLY_TRANSPARENT_MAX_RETRIES,
+ EARLY_TRANSPARENT_TOTAL_ATTEMPTS,
+ MIDSTREAM_RECOVERY_ATTEMPTS,
RecoveryHoldbackBuffer,
ToolSchema,
accept_tool_json_repair,
@@ -11,6 +14,15 @@ from core.anthropic.stream_recovery import (
)
+def test_early_transparent_retry_total_attempts_is_five() -> None:
+ assert EARLY_TRANSPARENT_TOTAL_ATTEMPTS == 5
+ assert EARLY_TRANSPARENT_MAX_RETRIES == 4
+
+
+def test_midstream_recovery_attempts_total_is_five() -> None:
+ assert MIDSTREAM_RECOVERY_ATTEMPTS == 5
+
+
def test_retryable_stream_error_classifies_transport_and_http_status() -> None:
assert is_retryable_stream_error(httpx.ReadError("cut off"))
diff --git a/tests/providers/test_anthropic_messages_429_retry.py b/tests/providers/test_anthropic_messages_429_retry.py
index 06d246d0..7b0fb0e0 100644
--- a/tests/providers/test_anthropic_messages_429_retry.py
+++ b/tests/providers/test_anthropic_messages_429_retry.py
@@ -196,7 +196,7 @@ async def test_native_stream_5xx_retry_exhausted(provider_config, status_code, s
):
events = [e async for e in provider.stream_response(req)]
- assert mock_send.await_count == 4
+ assert mock_send.await_count == 5
assert bad.is_closed
assert_canonical_stream_error_envelope(
events,
diff --git a/tests/providers/test_openai_compat_5xx_retry.py b/tests/providers/test_openai_compat_5xx_retry.py
index 657c0da6..a17c2a3f 100644
--- a/tests/providers/test_openai_compat_5xx_retry.py
+++ b/tests/providers/test_openai_compat_5xx_retry.py
@@ -106,7 +106,7 @@ async def test_nim_stream_openai_5xx_exhausted_emits_user_message(
mock_create.side_effect = _internal_5xx(status_code)
events = [e async for e in provider.stream_response(req)]
- assert mock_create.await_count == 4
+ assert mock_create.await_count == 5
blob = "".join(events)
assert expect_substr in blob.lower()
finally:
diff --git a/tests/providers/test_provider_rate_limit.py b/tests/providers/test_provider_rate_limit.py
index 33878541..fd40230c 100644
--- a/tests/providers/test_provider_rate_limit.py
+++ b/tests/providers/test_provider_rate_limit.py
@@ -4,7 +4,16 @@ import time
import pytest
import pytest_asyncio
-from providers.rate_limit import GlobalRateLimiter
+from providers.rate_limit import (
+ DEFAULT_UPSTREAM_MAX_RETRIES,
+ UPSTREAM_TRANSIENT_TOTAL_ATTEMPTS,
+ GlobalRateLimiter,
+)
+
+
+def test_upstream_transient_retry_total_attempts_is_five() -> None:
+ assert UPSTREAM_TRANSIENT_TOTAL_ATTEMPTS == 5
+ assert DEFAULT_UPSTREAM_MAX_RETRIES == 4
class TestProviderRateLimiter:
diff --git a/uv.lock b/uv.lock
index 34e58132..fd43800b 100644
--- a/uv.lock
+++ b/uv.lock
@@ -554,7 +554,7 @@ wheels = [
[[package]]
name = "free-claude-code"
-version = "1.2.39"
+version = "1.2.40"
source = { editable = "." }
dependencies = [
{ name = "aiohttp" },