how to get values of rate limits (same as x-ratelimit-remaining-requests,x-ratelimit-remaining-tokens,x-request-time)

Yuri Uiri 0 Reputation points
2025-01-15T09:23:46.9+00:00

For every network request, openai provides information about x-ratelimit-remaining-requests,x-ratelimit-remaining-tokens,x-request-time in the response headers. It would be nice to know how to get the values of these limits using axios/curl query.

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,626 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 17,176 Reputation points
    2025-01-15T14:41:15.1433333+00:00

    Hello Yuri Uiri,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you like to extract specific rate-limiting headers (x-ratelimit-remaining-requests, x-ratelimit-remaining-tokens, and x-request-time) from the OpenAI API response using Axios or Curl.

    If you would like to use Axios:

    const axios = require('axios');
    async function fetchHeaders() {
      try {
        const response = await axios.get('https://api.openai.com/v1/engines/davinci/completions', {
          headers: {
            'Authorization': `Bearer YOUR_API_KEY`, // Replace with your OpenAI API key
          },
        });
        const remainingRequests = response.headers['x-ratelimit-remaining-requests'];
        const remainingTokens = response.headers['x-ratelimit-remaining-tokens'];
        const requestTime = response.headers['x-request-time'];
        console.log('Remaining Requests:', remainingRequests || 'N/A');
        console.log('Remaining Tokens:', remainingTokens || 'N/A');
        console.log('Request Time:', requestTime || 'N/A');
      } catch (error) {
        console.error('Error:', error.response ? error.response.data : error.message);
      }
    }
    fetchHeaders();
    

    Replace YOUR_API_KEY with a valid API key and handle cases where headers might be absent by providing default values (N/A).

    If you would like to use Curl:

    curl -i https://api.openai.com/v1/engines/davinci/completions \ 
    -H "Authorization: Bearer YOUR_API_KEY"
    

    To extract specific headers:

    curl -i https://api.openai.com/v1/engines/davinci/completions \
    -H "Authorization: Bearer YOUR_API_KEY" | grep -i 'x-ratelimit-remaining-requests\|x-ratelimit-remaining-tokens\|x-request-time'
    

    The -H option sets the required authorization header, while grep filters the headers of interest.

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.