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
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.mjsRead the full example: examples/08_resilience.mjs →