Advanced Orchestration Engines ⏱ 12 min read 📁 examples/08_resilience.mjs

Lesson 27: Resilience, Retry Policies and Security

Enterprise resilience: classified retry policies, abort-aware exponential backoff, sliding-window stream timeouts, parameter warnings, and credential redaction.

1. Why This Feature Exists

Unstable network conditions, transient 503 errors, and token expiration must not bring down production agent workflows. Merci SDK embeds battle-tested resilience policies into every layer.

2. Key Symbols and Exports

RetryPolicy & Error Classification
Categorizes errors: transient network (retry), rate limit (backoff), auth/bad request (fatal immediately).
Abort-Aware Backoff
Sleeping retry delays cancel instantly when an AbortSignal fires.
Sliding-Window Stream Timeout
Monitors time between individual streamed chunks, not just total request time.
parameter_warning
Surfaces warnings when parameters are unsupported or degraded on active provider.
Credential Redaction
Automatically scrubs API keys, JWTs, and bearer tokens from logs and error dumps.

3. Implementation Walkthrough

import {
    ProviderType,
    resolveProvider,
    initializeClient,
    loadCredentials,
    getDefaultModel
} from 'merci-sdk';
import { RetryPolicy } from 'merci-sdk';

const PROVIDER = resolveProvider();
const { client } = await initializeClient({ provider: PROVIDER, options: loadCredentials({ provider: PROVIDER }) });

// Custom Resilience Policy
const policy = new RetryPolicy({
    maxRetries: 4,
    initialDelayMs: 500,
    maxDelayMs: 8000,
    backoffMultiplier: 2,
    retryableStatuses: [429, 500, 502, 503, 504]
});

// Sliding-window stream timeout: reset timer on every received chunk
const session = client.session(getDefaultModel({ provider: PROVIDER }))
    .withParameters({ streamChunkTimeoutMs: 3000 });

console.log('Resilience policy configured successfully.');

4. Wire and Telemetry Mechanics

When an HTTP request returns a retryable status, RetryPolicy calculates jittered exponential backoff, emits 'api_retry', and waits before retrying. Redaction filters wipe secrets from wire logs.

5. Run the Example

Execute the runnable example directly from the repository root:

node examples/08_resilience.mjs

Read the full example: examples/08_resilience.mjs →

Expected Output: Demonstrates deterministic retry classification, sliding timeouts, parameter warnings, and credential redaction.