Debugging requests
In addition to error codes returned from API responses, it may sometimes be necessary to inspect HTTP response headers as well. Of particular interest will be the headers which contain the unique ID of a particular API request, and information about rate limiting applied to your requests. Below is an incomplete list of HTTP headers returned with API responses:
API meta information
openai-organization
: The organization associated with the requestopenai-processing-ms
: Time taken processing your API requestopenai-version
: REST API version used for this request (currently2020-10-01
)x-request-id
: Unique identifier for this API request (used in troubleshooting)
x-ratelimit-limit-requests
x-ratelimit-limit-tokens
x-ratelimit-remaining-requests
x-ratelimit-remaining-tokens
x-ratelimit-reset-requests
x-ratelimit-reset-tokens
OpenAI recommends logging request IDs in production deployments, which will allow more efficient troubleshooting with our support team should the need arise. Our official SDKs provide a property on top level response objects containing the value of the x-request-id
header.
Request ID in Python
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
messages=[{
"role": "user",
"content": "Say this is a test",
}],
model="gpt-4o-mini",
)
print(response._request_id)
Request ID in JavaScript
import OpenAI from 'openai';
const client = new OpenAI();
const response = await client.chat.completions.create({
messages: [{ role: 'user', content: 'Say this is a test' }],
model: 'gpt-4o-mini'
});
console.log(response._request_id);
Access raw response objects in SDKs
If you are using a lower-level HTTP client (like fetch or HttpClient
in C#), you should already have access to response headers as a part of the HTTP interface.
If you are using one of OpenAI's official SDKs (which largely abstract the HTTP request/response cycle), you will need to access raw HTTP responses in a slightly different way.
Below is an example of accessing the raw response object (and the x-ratelimit-limit-tokens
header) using our Python SDK.
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.with_raw_response.create(
messages=[{
"role": "user",
"content": "Say this is a test",
}],
model="gpt-4o-mini",
)
print(response.headers.get('x-ratelimit-limit-tokens'))
# get the object that `chat.completions.create()` would have returned
completion = response.parse()
print(completion)
Here is how you'd access a raw response (and the x-ratelimit-limit-tokens
header) using our JavaScript SDK.
import OpenAI from 'openai';
const client = new OpenAI();
const response = await client.chat.completions.create({
messages: [{ role: 'user', content: 'Say this is a test' }],
model: 'gpt-4o-mini'
}).asResponse();
// access the underlying Response object
console.log(response.headers.get('x-ratelimit-limit-tokens'));