API Doc-ElkAPI
  1. Best Practices
API Doc-ElkAPI
  • Quick Start
    • Quick Start
    • Must-read for beginners
    • ElkAPI Announcement
  • API Reference
    • Error status code description
    • OpenAI Format
      • Veo3-chat format
      • seedance
      • response
      • generate video
      • image
      • Image Editing (gpt-image-1)
      • embed
      • Realtime
      • Get model list
      • Response ID to obtain results
      • Hunyuan3D
    • Anthropic Format
      • Anthropic Claude
    • Midjourney Image Generation
      • Quick teaching-complete process in one go
      • Task Query
        • pagination query
        • Specify ID to obtain task
      • Submit Imagine task
      • Submit video task
      • Submit editing task
      • Submit Action Task
      • Submit Blend task
      • Submit Describe task
      • Submit Modal
    • Image Generation
      • ideogram(images)
        • Official documentation (updated in real time)
        • Generate 3.0 (illustrated in the text)
        • Reframe 3.0 (Refactoring)
        • Replace Background 3.0
        • Remix 3.0 (Mixed Graph)
        • Edit 3.0 (Edit)
        • Generate (text and image)
        • Remix (mixed image)
        • Upscale (enlarged high-definition)
        • Describe
        • Edit
      • Flux(images)
        • Create Task - General
        • generate image
        • Fine tuning (temporarily unavailable)
        • query task
        • Generate image (replica format)
      • Replicate(image)
        • Create Task - General
        • Create task - float kontext pro, max
        • Create task-black forest labs/flux-1.1-pro
        • Create task-black forest labs/flux-1.1-pro ultra
        • query task
        • Create task
      • Recraft(images)
        • Generate Image
        • Vectorize Image
        • Remove Background
        • Clarity Upscale
        • Create style
        • Generative Upscale
    • Music Generation
      • Suno
        • Set suno version
        • Suno API Scenario Application Guide
        • Generate lyrics
        • Generate music
        • Upload music
        • Song Splicing
        • Full track acoustic separation
        • Single track acoustic separation
        • Create a new Persona
        • Query a single task
        • Generate MP4 MV video
        • Retrieve WAV format files
        • Timing: Lyrics, audio timeline
        • Batch query task
        • Create music using persona_id
      • Udio (not available yet)
        • Generate music
        • Query a single task
    • Video Generation
      • veo3
        • veo3-chat format
        • Submit video generation task
        • Check the video generation status
      • Dream Test
        • seedance
      • runway(video)
        • Official format
          • Generate videos from images
          • Get detailed task information
        • Reverse format
          • Generate (text)
          • Generate (refer to image)
          • Video2video video to video style redrawing
          • Act one emoji transfer
          • Feed - Get Task
      • kling (video)
        • Callback Protocol
        • image expansion
        • generate image
        • text-to-video
        • image-to-video
        • Video extension
        • Virtual try on
        • lip-sync
        • video effects
        • Query task (single)
      • luma (video)
        • Official API format
          • generate video
          • Single query task
      • MiniMax Conch (video)
        • Official Documentation
        • video generation
        • query task
        • File Download
      • PIKA(video)
        • Universal version
          • generate video
          • Query video tasks
        • Generate (reference video/extended video)
        • Generate (refer to image)
        • Generate (text)
        • feed
      • sora
        • Reverse format
          • Create video
          • Generate Video
          • Query video tasks
    • Audio
      • Text to audio conversion
      • audio to text
      • Create translation
  • Integration Guide
    • Best Practices
      • Claude code access instructions
      • ElkAPI Account Balance Query API Usage Instructions
      • Retry logic documentation using ElkAPI and OpenAI's official API
      • Midjorney Calling Best Practices
      • Runway Call Best Practices
    • Application Integration Guide
  • Pricing and Billing
    • About Price
    • About Grouping
  • Help Center
    • Help Center
    • Interface stability
    • Common Misconceptions
    • Use confusion
    • Privacy Policy
    • Terms of Service
  1. Best Practices

Retry logic documentation using ElkAPI and OpenAI's official API

Introduction#

This document aims to help you understand how to implement automatic request switching and retry logic when using ElkAPI and the OpenAI official API. Requests are prioritized through the ElkAPI. If an error response is received, the request is automatically switched to the OpenAI official API and retried.

Retry Logic Overview#

This retry logic consists of two main steps:
1.
Prioritize ElkAPI: First, the API request is attempted using the ElkAPI. If the request succeeds, a response is returned. If the request fails (for example, due to an HTTP error), the second step is performed.
2.
Switch to OpenAI Official API: If a ElkAPI request fails, the program automatically switches to the OpenAI official API for the same request. If the request succeeds, a response is returned. If the request fails, an error message is returned to the user.

Implementation Details#

Environment Setup#

Before beginning implementation, please ensure that you have installed the necessary Python libraries. This example uses the OpenAI Python client library, so you'll need to make sure it's installed:

Implementation Steps#

Here are the specific implementation steps:
1.
Initialize the API Client:
Create a ElkAPI client instance, setting its base_url and api_key.
Create an OpenAI official API client instance, only setting the api_key.
2.
Implement the Request Logic:
Attempt to make a request using the ElkAPI client.
Catch any API errors (such as 400 or 500 errors). These errors are typically caused by a failed request or an error code in the response.
If an error occurs, switch to the OpenAI official API client and make the same request.
For each request method, log and output the error message (if any).
3.
Return a Response or Error Message:
If any request method succeeds, return the request result message.
If both request methods fail, a string containing the error message is returned.

Sample code#

The following is a Python sample code that implements the retry logic:

Notes#

Error handling: In a production environment, expand the error log to better handle and log errors.
Performance monitoring: Frequently switching to the OpenAI official API may indicate a problem with the ElkAPI, which requires further investigation and optimization.
The implementation of this retry logic is intended to improve the robustness of the system and the request success rate, ensuring that the system can still provide services normally when problems occur at specific API nodes.
Previous
ElkAPI Account Balance Query API Usage Instructions
Next
Midjorney Calling Best Practices
Built with