{"meta":{"title":"Handling API rate limits","intro":"Copilot Chat can help handle API rate limits by suggesting code that detects them and implements retry logic.","product":"GitHub Copilot","breadcrumbs":[{"href":"/en/copilot","title":"GitHub Copilot"},{"href":"/en/copilot/tutorials","title":"Tutorials"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat Cookbook"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/debug-errors","title":"Debug errors"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/debug-errors/handle-api-rate-limits","title":"Handle API rate limits"}],"documentType":"article"},"body":"# Handling API rate limits\n\nCopilot Chat can help handle API rate limits by suggesting code that detects them and implements retry logic.\n\nWhen making requests to APIs, it's common to encounter rate limits that restrict the number of calls you can make within a certain time frame. GitHub Copilot Chat can help you handle these limits by suggesting code to detect rate limit responses and automatically retry requests after a delay.\n\n## Example scenario\n\nThe following Python code fetches weather data from an external API. If the API has rate limits, requests may fail when limits are exceeded, and your app may need a way to handle these responses gracefully by implementing retry logic.\n\n```python id=rate-limit-example\nfrom flask import Flask, request\nimport requests\n\napp = Flask(__name__)\n\nWEATHER_API_URL = \"https://api.example.com/weather\"\n\n@app.route('/get_weather', methods=['GET'])\ndef get_weather():\n    city = request.args.get('city')\n    # Simulate an API request to the external weather service\n    response = requests.get(WEATHER_API_URL, params={\"city\": city})\n    weather_data = response.json()\n\n    return weather_data\n```\n\n## Example prompt\n\n```copilot copy prompt ref=rate-limit-example\nHow can I handle API rate limits within get_weather().\n```\n\n## Example response\n\n> \\[!NOTE] The following response is an example. Copilot Chat responses are non-deterministic, so you may get a different response from the one shown here.\n\nCopilot might suggest code that implements a retry mechanism with exponential backoff to limit the frequency of retry attempts.\n\nFor example:\n\n```python\nimport requests\nfrom flask import Flask, request\nfrom requests.adapters import HTTPAdapter\nfrom requests.packages.urllib3.util.retry import Retry\n\napp = Flask(__name__)\n\nWEATHER_API_URL = \"https://api.example.com/weather\"\n\ndef requests_retry_session(\n    retries=3,\n    backoff_factor=0.3,\n    status_forcelist=(500, 502, 504),\n    session=None,\n):\n    session = session or requests.Session()\n    retry = Retry(\n        total=retries,\n        read=retries,\n        connect=retries,\n        backoff_factor=backoff_factor,\n        status_forcelist=status_forcelist,\n    )\n    adapter = HTTPAdapter(max_retries=retry)\n    session.mount(\"http://\", adapter)\n    session.mount(\"https://\", adapter)\n    return session\n\n@app.route('/get_weather', methods=['GET'])\ndef get_weather():\n    city = request.args.get('city')\n    try:\n        response = requests_retry_session().get(WEATHER_API_URL, params={\"city\": city})\n        response.raise_for_status()\n        weather_data = response.json()\n    except requests.exceptions.RequestException as e:\n        return {\"error\": str(e)}, 500\n\n    return weather_data\n```\n\nIn this example, Copilot's suggestions include setting up a retry session that allows the code to automatically retry requests if they fail due to specific status codes (500, 502, 504). The `backoff_factor` gradually increases the delay between retries, helping avoid exceeding the API's rate limit further.\n\n## Further reading\n\n* [Prompt engineering for GitHub Copilot Chat](/en/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [Best practices for using GitHub Copilot](/en/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}