Skip to content

Add retry mechanism for API calls#6

Open
0xperp wants to merge 2 commits into
mainfrom
add-retry-utility
Open

Add retry mechanism for API calls#6
0xperp wants to merge 2 commits into
mainfrom
add-retry-utility

Conversation

@0xperp

@0xperp 0xperp commented Mar 9, 2025

Copy link
Copy Markdown

Closes #3

This PR implements a robust retry mechanism for API calls to improve reliability when interacting with external services like the Anthropic API. It includes:

  • A withRetry utility function that wraps async operations with configurable retry logic
  • Exponential backoff for retries to reduce load during outages
  • Configurable retry policies (max attempts, delays, retry conditions)
  • Unit tests to verify the retry mechanism works correctly

Usage Example

// In anthropic.ts
import { withRetry } from './utils/retry';

// Create a retry-enabled version of the API call
const callAnthropicWithRetry = withRetry(
  client.logAndCreateChatCompletion.bind(client),
  {
    maxRetries: 3,
    initialDelay: 1000,
    backoffFactor: 2,
    isRetriable: (error) => {
      // Retry on rate limit errors or network issues
      return error.status === 429 || error.code === 'ECONNRESET';
    }
  }
);

// Use the retry-enabled function instead of the regular API call
const result = await callAnthropicWithRetry(params);

This enhancement will make the bot more resilient to temporary network issues, rate limits, and other transient API errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement error handling and retry mechanism for API calls

1 participant