Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(anthropic): wait out a rate limit instead of failing claude code syncs at once (#114879)

Tom Owers96030665794c
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py
index 090be7afdd4b..9d774ac16fbe 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/rest_client.py
@@ -216,13 +216,25 @@ def _seconds_from_rfc3339_reset(value: str) -> Optional[float]:
return (reset_at - datetime.now(UTC)).total_seconds()
+def _seconds_until_anthropic_replenish(value: str) -> Optional[float]:
+ # Anthropic documents this instant as the time when the request budget is fully replenished,
+ # not the time when the next request is allowed. The bucket refills continuously, so a request
+ # usually succeeds long before the instant. An uncapped value of up to an hour made the client
+ # fail every 429 at once, so the value is capped at the longest wait the client honors. The
+ # client then waits and retries instead.
+ seconds = _seconds_from_rfc3339_reset(value)
+ if seconds is None:
+ return None
+ return min(seconds, max_retry_after_seconds())
+
+
# Rate-limit reset headers to fall back on when a 429 carries no ``Retry-After``, each paired with
# the parser that turns its value into "seconds from now until the budget replenishes".
_RATE_LIMIT_RESET_HEADERS: tuple[tuple[str, Callable[[str], Optional[float]]], ...] = (
# Anthropic rate limits per organization and documents this RFC 3339 instant as when the request
# budget replenishes. Its Admin API (the usage/cost report endpoints an Anthropic source pages
# through) answers 429 without a ``Retry-After``, so this is the only delay it advertises.
- ("anthropic-ratelimit-requests-reset", _seconds_from_rfc3339_reset),
+ ("anthropic-ratelimit-requests-reset", _seconds_until_anthropic_replenish),
# Sentry signals its rate-limit window with a UNIX epoch timestamp rather than ``Retry-After``,
# and Sentry's flat / fan-out endpoints (e.g. ``project_users``) sync through this client too.
("X-Sentry-Rate-Limit-Reset", _seconds_from_epoch_reset),
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py
index d88d3dc4755c..7d073068a0b2 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/rest_source/tests/test_rest_client.py
@@ -5,6 +5,8 @@ from typing import Any
import pytest
from unittest.mock import MagicMock, patch
+from django.test import override_settings
+
from parameterized import parameterized
from requests import Response
from requests.exceptions import ChunkedEncodingError, ProxyError, ReadTimeout, TooManyRedirects
@@ -816,13 +818,16 @@ class TestRESTClient:
[
("rfc3339_utc", "2026-03-06T12:00:45Z", 45.0),
("rfc3339_offset", "2026-03-06T12:01:00+00:00", 60.0),
- ("far_future", "2026-03-06T13:00:00Z", 3600.0),
+ # The header says when the bucket is full again, which can be an hour away while the
+ # next request is already allowed, so a far reset waits the longest honored delay.
+ ("far_future", "2026-03-06T13:00:00Z", 300.0),
# A window that has already cleared, or a value we can't read, tells us nothing — the
# caller falls back to exponential backoff rather than retrying with no delay at all.
("already_elapsed", "2026-03-06T11:59:55Z", None),
("unparseable", "in a bit", None),
]
)
+ @override_settings(DATA_WAREHOUSE_SOURCE_MAX_RETRY_AFTER_SECONDS=300.0)
@patch("products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client.datetime")
def test_parse_retry_after_honors_anthropic_rate_limit_reset(
self, _name: str, header_value: str, expected: float | None, mock_datetime