{"meta":{"title":"Handling API rate limits","intro":"공동 파일럿 채팅 can help handle API rate limits by suggesting code that detects them and implements retry logic.","product":"GitHub Copilot","breadcrumbs":[{"href":"/ko/copilot","title":"GitHub Copilot"},{"href":"/ko/copilot/tutorials","title":"자습서"},{"href":"/ko/copilot/tutorials/copilot-cookbook","title":"GitHub Copilot Cookbook"},{"href":"/ko/copilot/tutorials/copilot-cookbook/debug-errors","title":"Debug errors"},{"href":"/ko/copilot/tutorials/copilot-cookbook/debug-errors/handle-api-rate-limits","title":"Handle API rate limits"}],"documentType":"article"},"body":"# Handling API rate limits\n\n공동 파일럿 채팅 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. 깃허브 코파일럿 채팅 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] 다음 응답이 예제입니다. 공동 파일럿 채팅 응답은 비결정적이므로 여기에 표시된 응답과 다른 응답을 얻을 수 있습니다.\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* [GitHub Copilot 채팅에 대한 프롬프트 엔지니어링](/ko/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [GitHub 부필로트 사용에 대한 모범 사례](/ko/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}