Configuring LLM API Access: Keys, Models, and Rate Limiting
A practical guide to connecting local AI development tools to external LLM APIs. Covers how to obtain and store API keys securely, query available models, write a YAML configuration file, set rate limits with retry logic, and troubleshoot the most common connection failures.
1. The Big Picture: Five Steps and Three Common Pitfalls
Calling external models from a local AI development tool involves five steps: obtaining an access key, querying available models, writing the model configuration file, setting rate limits, and verifying connectivity. Each step depends on the result of the previous one, so the order matters.
When configuration fails, the cause usually falls into one of three areas: an invalid key or insufficient permissions, a model ID that doesn’t match what the server expects, or a missing version path in the endpoint URL. Checking these three points resolves most issues.
All URLs, key names, and model IDs in this article use placeholders such as https://api.example.com/v1 and your-model-id. Replace them with the real values from your provider. The examples assume a server that is compatible with the common OpenAI-style API. If your provider’s API differs, you only need to adjust the request paths and field names.
2. Built-in Models vs. External API Access
| Dimension | Built-in Models | Third-Party API Access |
|---|---|---|
| Setup effort | Works out of the box | Requires a key and configuration file |
| Model selection | Limited options | Choose from multiple models as needed |
| Cost control | Determined by the tool | Controlled via model choice and rate limits |
| Troubleshooting | Relatively easy | Involves networking, authentication, and model IDs |
If you only use AI features occasionally, built-in models are usually enough. Consider external services when you need a specific model or want control over response speed and cost.
3. Obtaining and Safeguarding Your API Key
Log in to your provider’s developer console, go to the credentials page, and create a key. Most platforms display the full key only once at creation time; afterward you can only see the last few characters. Copy it immediately and store it in a password manager or your organization’s secret management service.
Follow these rules when handling keys:
- Never hardcode keys in source code. Configuration files should only reference environment variable names.
- Never commit keys to Git. Add files containing keys to
.gitignore. - Never paste full keys into logs, screenshots, support tickets, or chat messages.
- Create separate keys per project or device, and grant only the permissions you need.
To set an environment variable, macOS and Linux users can add the following to their shell configuration file:
export MODEL_API_KEY="your-api-key-here"
Windows users can add a MODEL_API_KEY system environment variable and then restart the editor.
If you suspect a key has been leaked, revoke it in the console immediately and create a replacement. Once revoked, the old key stops working. The call history in the console can help you determine whether the key was misused. Rotating keys regularly—for example, every three months—also reduces long-term exposure risk.
4. Querying the Available Model List
The model ID in your configuration file must exactly match what the server expects, so start by querying the available models. Most compatible APIs expose a model list endpoint you can call directly with curl:
curl https://api.example.com/v1/models \-H "Authorization: Bearer $MODEL_API_KEY"
The id field in the response is the model ID. Note down the id you need, for example your-model-id, and use it verbatim in your configuration file. Preserve capitalization, hyphens, and version numbers exactly—don’t rewrite them from memory.
If the list comes back empty, your key most likely lacks permission to use any models. Return to the console and check the permission settings.
5. Writing the Model Configuration File
The configuration file uses YAML, which is readable and easy to keep under version control. Here is a reusable template:
provider:name: example-providerbase_url: https://api.example.com/v1api_key_env: MODEL_API_KEYtimeout_seconds: 30models:- id: your-model-iddisplay_name: General Chat Modelmax_tokens: 2048temperature: 0.7rate_limit:requests_per_minute: 60max_concurrency: 4retry:max_attempts: 3backoff_seconds: 2
Key fields explained:
base_url: The API root URL, usually ending in/v1. Omitting the version path is a common mistake.api_key_env: The name of the environment variable holding the key. The program reads this variable at startup; the key itself never appears in the file.timeout_seconds: Per-request timeout. Increase it for slow networks, but staying under 120 seconds is recommended.id: The model ID, which must match the value returned by the model list endpoint.max_tokens: Maximum length of a single response; it affects both cost and wait time.temperature: Output randomness. For code generation tasks, use a lower value, such as 0.2 to 0.5.
Restart the development tool after changing the configuration file for the changes to take effect.
6. Setting Rate Limits
Rate limiting serves two purposes. First, it keeps you within the provider’s quota so requests aren’t rejected. Second, it controls cost, preventing a runaway script from generating a large bill through repeated calls.
In the template, requests_per_minute caps how many requests you can send per minute, while max_concurrency caps how many can be in flight simultaneously. Use them together: one limits frequency, the other limits concurrency.
If your provider specifies exact quotas, follow their documentation. Set your local limits slightly below the quota to leave headroom.
When you receive a 429 response, the client should wait and retry, increasing the wait each time. In the template, backoff_seconds: 2 means waiting 2 seconds before the first retry, then doubling with each subsequent attempt. Don’t retry too many times, or you’ll add load to the server.
Here is a simplified Python example showing how to throttle request frequency and handle retries according to the configuration:
```python
import os
import time
import requests
BASE_URL = “https://api.example.com/v1“
API_KEY = os.environ[“MODEL_API_KEY”]
REQUESTS_PER_MINUTE = 60
MIN_INTERVAL = 60 / REQUESTS_PER_MINUTE # Minimum interval between requests (seconds)
MAX_ATTEMPTS = 3
BACKOFF = 2
_last_call = 0.0
def call_model(payload: dict) -> dict:
global _last_call
headers = {“Authorization”: f”Bearer {API_KEY}”}
for attempt in range(1, MAX_ATTEMPTS + 1):
wait = MIN_INTERVAL - (time.time() - _last_call)
if wait > 0:
time.sleep(wait)
_last_call = time.time()
resp = requests.post(
f”{BASE_URL}/chat/completions”,
headers=headers,
json=payload,
timeout=30,
)
if resp.status_code == 429 and attempt < MAX_ATTEMPTS:
time.sleep(BACKOFF * attempt)
continue
