Skip to main content

Retry With Backoff (Rust)

High-performance retry and backoff policy engine for ContextForge and MCP Gateway.

Features

  • Rust-backed retry state tracking for tool invocations
  • Exponential backoff with optional jitter
  • Per-tool policy overrides without duplicating whole plugin configs
  • Retry decisions based on isError, structured status_code, or optional parsed text payloads
  • Automatic state eviction for stale request entries
  • Gateway ceiling enforcement for max_retries
  • Retry policy metadata returned on tool and resource hooks

Build

make install

Runtime Requirements

This plugin depends on cpex>=0.1.0,<0.2 and imports hook models from cpex.framework. The compiled Rust extension is mandatory; there is no Python fallback implementation.

Usage

The plugin runs on tool_post_invoke and resource_post_fetch.

Typical uses:

  • Retry transient upstream failures such as 429, 500, 502, 503, and 504
  • Clamp aggressive plugin settings to the gateway-wide retry ceiling
  • Apply stricter retry budgets to fragile or expensive tools

Configuration

Core settings

  • max_retries: maximum retry attempts before giving up
  • backoff_base_ms: base delay for exponential backoff
  • max_backoff_ms: upper bound for computed retry delays
  • retry_on_status: HTTP or structured status codes treated as retriable
  • jitter: randomize delay within the current exponential ceiling
  • check_text_content: inspect text content for JSON-encoded error payloads when structured content is absent

Per-tool overrides

Use tool_overrides to change retry behavior for a specific tool:

  • max_retries
  • backoff_base_ms
  • max_backoff_ms
  • retry_on_status
  • jitter

Behavior Notes

  • Successful responses clear retry state for the (tool, request_id) pair.
  • Retry state expires after a short TTL so abandoned request state does not accumulate indefinitely.
  • If check_text_content is disabled, the hot path uses the Rust state manager directly.
  • If check_text_content is enabled, Python-side payload inspection supplements the Rust state manager before applying retry policy.

Returned Metadata

tool_post_invoke — OpenTelemetry metrics

tool_post_invoke accepts an optional extensions parameter carrying OpenTelemetry trace context. When a trace context is present (via extensions.request.trace_id), the plugin emits operational metrics on result.metadata["retry_with_backoff"] with the following schema:

result.metadata["retry_with_backoff"] = {
    "retry_count": 1,      # int — consecutive_failures after this call's outcome is recorded; 0 on success
    "retry_delay_ms": 100, # int — the per-attempt delay computed for this call; 0 on success or once exhausted
}

Every call (success, within-budget retry, or exhausted) has a meaningful outcome to report, including the all-zero success case — there is deliberately no total_backoff_ms cumulative counter, only these two per-call fields.

Gating: Metrics are only emitted when a valid trace_id is present in the trace context (extensions.request.trace_id). No trace context means no result.metadata write at all, regardless of any config flag.

resource_post_fetch — unchanged config echo

resource_post_fetch is out of scope for the OTel metrics contract above — it never receives extensions and continues to unconditionally emit the plugin's active retry policy configuration (not per-call outcome data) on result.metadata:

  • max_retries
  • backoff_base_ms
  • max_backoff_ms
  • retry_on_status

Migration Note

Version 0.3.6 is a breaking change for tool_post_invoke consumers only (resource_post_fetch is unaffected):

  • tool_post_invoke no longer emits the flat, unconditional retry_policy config echo (max_retries, backoff_base_ms, max_backoff_ms, retry_on_status). That echo is replaced by the namespaced, trace-gated result.metadata["retry_with_backoff"] schema above (retry_count, retry_delay_ms).
  • tool_post_invoke now accepts a new optional extensions parameter carrying OpenTelemetry trace context. Emission is gated solely on extensions.request.trace_id being present and valid — if no trace context is supplied, no metrics are written at all.
  • Consumers that previously read the config echo from tool_post_invoke unconditionally must migrate to reading result.metadata["retry_with_backoff"] and must pass a trace_id via extensions to receive metrics.
  • resource_post_fetch keeps its pre-existing, un-namespaced config echo byte-for-byte unchanged — it is a different contract, not covered by this migration.

Testing

# Full plugin CI
make ci

Performance

The retry state manager is implemented in Rust so the common retry decision path avoids Python bookkeeping overhead for normal structured tool results.

Metadata

Release files for cpex-retry-with-backoff 0.3.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cpex-retry-with-backoff 0.3.13
File Size Uploaded Provenance Actions
cpex_retry_with_backoff-0.3.13.tar.gz 43.2 kB With provenance Details for cpex_retry_with_backoff-0.3.13.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for cpex-retry-with-backoff 0.3.13
File Actions
cpex_retry_with_backoff-0.3.13-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for cpex_retry_with_backoff-0.3.13-cp311-abi3-win_amd64.whl
cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_x86_64.whl CPython 3.11 abi3 Linux glibc 2.34+ x86-64 Details for cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_x86_64.whl
cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_s390x.whl CPython 3.11 abi3 Linux glibc 2.34+ IBM System/390x Details for cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_s390x.whl
cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_ppc64le.whl CPython 3.11 abi3 Linux glibc 2.34+ PowerPC 64-le Details for cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_ppc64le.whl
cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_aarch64.whl CPython 3.11 abi3 Linux glibc 2.34+ ARM64 Details for cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_aarch64.whl
cpex_retry_with_backoff-0.3.13-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for cpex_retry_with_backoff-0.3.13-cp311-abi3-macosx_11_0_arm64.whl

Total release size: 1.7 MB

Release files / cpex_retry_with_backoff-0.3.13.tar.gz

Download URL cpex_retry_with_backoff-0.3.13.tar.gz
Size 43.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3bde22a7f7c6f0a18580db8a920adf3e1898934652d4e84c42562980e2ba9875
BLAKE2b-256 checksum
How to use checksums
4bc4a59d48074925d64b6501a9c48e8bc31cc02683dcd68378c7170d7b240db5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / cpex_retry_with_backoff-0.3.13-cp311-abi3-win_amd64.whl

Download URL cpex_retry_with_backoff-0.3.13-cp311-abi3-win_amd64.whl
Size 208.4 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
748db946d37f5ce7fbf84e96c2e6a0e4d6feda3071da532fd38040acda5c5849
BLAKE2b-256 checksum
How to use checksums
13e123007a47ebba29ab417346c46f405cd90629142f50d36f643afab8184738
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_x86_64.whl

Download URL cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_x86_64.whl
Size 286.4 kB
Tags CPython 3.11 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
77fe6c62e53fb2631a8c511cef058110f121db6fa8a46dc156af9ddcc2de9d46
BLAKE2b-256 checksum
How to use checksums
e399cecf606beba4f01b87f79514af3254da7d6114fdd68a1da8c45b2181afce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_s390x.whl

Download URL cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_s390x.whl
Size 314.9 kB
Tags CPython 3.11 Linux glibc 2.34+ IBM System/390x abi3
SHA-256 checksum
How to use checksums
60c85f2dca98b28c8e808b4f196cc1baff98c5e17282f7f844ccece8392d75ff
BLAKE2b-256 checksum
How to use checksums
077548f769d8c0a406d283473947af3adbfc67939356c1bdf4d0ccdfb2f6def7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_ppc64le.whl

Download URL cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_ppc64le.whl
Size 310.6 kB
Tags CPython 3.11 Linux glibc 2.34+ PowerPC 64-le abi3
SHA-256 checksum
How to use checksums
bb585aea021b61967300193e2e0691ed774b55d6903187fda9bbcaa092ddd78e
BLAKE2b-256 checksum
How to use checksums
096e99e229197a2a90edda68aab43dadb52ff3bc97734732b84a66df792df15b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_aarch64.whl

Download URL cpex_retry_with_backoff-0.3.13-cp311-abi3-manylinux_2_34_aarch64.whl
Size 270.5 kB
Tags CPython 3.11 Linux glibc 2.34+ ARM64 abi3
SHA-256 checksum
How to use checksums
d353e41d273ad4f3a459416a7b66caa10c67b4c767ac3d5f98ab8e411f9320f2
BLAKE2b-256 checksum
How to use checksums
d20a47dd91649914fc1abe9a4bc6e62f4f09669c13e9c81f4f5f5b5dd6bf9a88
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / cpex_retry_with_backoff-0.3.13-cp311-abi3-macosx_11_0_arm64.whl

Download URL cpex_retry_with_backoff-0.3.13-cp311-abi3-macosx_11_0_arm64.whl
Size 259.5 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a103ccc13e5975d082e5b1de9b7d9a4b936a9cffdde3d693575e0c559a8c1c3d
BLAKE2b-256 checksum
How to use checksums
982c6174c2de335e153468753dfb7dcbcbeaa98424f3967d5db389c9194444f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for cpex-retry-with-backoff 0.3.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 7 files in this release have been uploaded with provenance metadata.

Select from 7 files

Attestation for cpex_retry_with_backoff-0.3.13.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page