- Cases that timed out (no response from the model)
- Cases that returned a transient error from the model
1. Import and Authenticate Portkey Client SDK
Portkey forwards your requests to your desired model and relays the response to your app. Portkey’s Client SDK is one of several ways to make those API calls through the AI gateway. To install it, type the following in your NodeJS environment:Portkey
and instantiate it using the Portkey API Key
portkey
instance with apiKey
and virtualKey
parameters. You can find the arguments for both of them in your Portkey Dashboard.
Visit the reference to obtain the Portkey API key and learn how to create Virtual Keys.
2. Gateway Configs to Automatically Retry
For the AI gateway to understand that you want to apply automatic retries to your requests, you must pass Gateway Configs in your request payload. Gateway Configs can be a JS Object or a JSON string. A typical Gateway Config to automatically retry three times when you hit rate-limits:retry
object with attempts
and on_status_codes
keys. The value of attempts
can be bumped up to 5
times to retry automatically, while on_status_codes
is an optional key. By default, Portkey will attempt to retry on the status codes [429, 500, 502, 503, 504]
.
Refer to the 101 on Gateway Configs and Automatic Retries.
3. Make API calls using Portkey Client SDK
You are now ready to make an API call through Portkey. While there are several ways to make API calls, in this cookbook, let’s pass the gateway configuration during the chat completion call.4. View the Logs
Now that you successfully know how to make API calls through Portkey, it’s also helpful to learn about Logs. You can find all requests sent through Portkey on the Dashboard > Logs page. This page provides essential information such as time, cost, and response. Feel free to explore it! Instead of using your own application-level looping or control structures to implement retries, you can use Portkey’s Gateway Configs to manage all of them.See the full code
See the full code