sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[PORT] Move Strands TS retry system to Python

Open notowen333 opened this issue 4 months ago • 3 comments

Source SDK

strands-ts

Target SDK

strands-py

Related Files

The retry system is fundamentally different in TS than Python. We have a base abstract class and then a Default<> class which implements it. In the constructor we have forwards compat for new retry types like ToolRetryStrategy when we expand RetryStrategy. We also decompose BackoffStrategies.

Ideally we fully mirror this system in python. The biggest question is how to vend this in a forwards compatible way. If it's the cleanest; it's OK to say ModelRetryStrategy | RetryStategy | [RetryStrategy] 

Can decide if there's a smoother back compat story, but the solution MUST be backwards compatible for existing ModelRetryStrategy

File paths:

strands-py/src/strands/agent/agent.py
strands-py/src/strands/event_loop/_retry.py
strands-ts/src/retry/*

Confirmation

  • [x] The change is complete in the source SDK, and every related artifact is listed above (source, tests, and docs, if the change has any).

notowen333 avatar Jul 20 '26 20:07 notowen333

Related to https://github.com/strands-agents/harness-sdk/issues/2498

liramon2 avatar Jul 23 '26 21:07 liramon2

I'd like to help land this. #4960 (cc @jimbrub) already ports the backoff strategies, RetryDecision, and an overridable compute_retry_decision on ModelRetryStrategy. What's left for parity is the agent-level shape this issue calls the biggest question. Proposal, to land after #4960:

1. A RetryStrategy type in strands.retry. Today it's just ModelRetryStrategy; it becomes a union when a ToolRetryStrategy exists. Same as TS's type RetryStrategy = ModelRetryStrategy.

2. Agent(retry_strategy=...) accepts a strategy or a list.

retry_strategy: RetryStrategy | Sequence[RetryStrategy] | None = <default>
  • A single ModelRetryStrategy behaves exactly as it does now, so existing code is unaffected.
  • With a list, each strategy is registered. If two share a type, only the first is used and a warning is logged, matching TS's warnOnDuplicateRetryStrategyTypes.
  • None keeps today's meaning (no retries).

3. Keep ModelRetryStrategy concrete, as #4960 does, rather than mirror TS's abstract ModelRetryStrategy + DefaultModelRetryStrategy split. Renaming the class people already pass in would break them.

4. Keep agent._retry_strategy pointing at the model strategy (the first ModelRetryStrategy in the list). Two internal paths read it: the backwards-compatible throttle event in event_loop.py, and router.py's _reset_retry_state() when it switches models. They'd keep working unchanged.

Questions before I start:

  • Is it OK to do this as a follow-up after #4960, rather than fold it into that PR?
  • None currently becomes ModelRetryStrategy(max_attempts=1) internally, so the router and event-loop paths always have a strategy. Keep that, or make None mean "no strategy" and guard those two paths?
  • Do you want a DefaultModelRetryStrategy alias in Python for name parity with TS, or is the concrete ModelRetryStrategy enough?

I'd expect roughly 100–150 lines plus tests.

AkshayShah03 avatar Oct 13 '26 22:10 AkshayShah03

Thanks @AkshayShah03. Agreed on using attempt_count rather than a public reset(), since it also covers hooks. It's also how TS already works: the agent loop restarts attemptCount at 1 on a model switch, and the built-in strategy budgets from that count, so the router never touches the strategy.

One thing to fold in so the built-in strategy doesn't regress: ModelRetryStrategy also keeps _last_delay (the escalated backoff) and _first_failure_at (behind BackoffContext.elapsed). Right now the router's _reset_retry_state() call is what clears those. If we drop that call and only restart attempt_count, the fallback inherits the primary's backoff. TS handles this with a no-op onFirstModelAttempt() hook, which the base class calls when attemptCount === 1 (strands-ts/src/retry/model-retry-strategy.ts). A Python on_first_model_attempt() would cover the timing state and give subclasses with their own state somewhere to reset it.

#4960 already moves the hooks.mdx example to event.attempt_count, so the docs side of this is already done.

I can work with you further on this once #4960 lands.

jimbrub avatar Oct 15 '26 13:10 jimbrub