Extending LLM Conversations via API After Free Tier Limits
Learn how to migrate long-running LLM chat sessions from free web and app tiers to a paid API, covering key setup, cost estimation, conversation migration, sample requests, and error troubleshooting.
When a large language model platform tightens its free tier, web and mobile clients often start showing issues: long conversations can no longer continue, input length per message gets capped, or daily message counts hit their ceiling. For users with extensive context who rely on continuous dialogue to drive projects, this can bring work to a halt.
Instead of waiting for free quotas to reset, you can switch to the platform’s official API and pay for actual usage. Once you understand a few fundamental concepts, you can resume conversations at a lower cost and gain more control over the process. This article covers the migration in the order you’ll need it: understanding the difference between API and client, creating an API key, estimating costs, migrating historical conversations, and finally providing a code example and troubleshooting guide.
1. Understand the Difference Between API and Client
An API (Application Programming Interface) is a defined communication protocol between two software systems. Web interfaces and mobile apps are clients: they handle login, UI rendering, and user-specific rate limits. When you call an API directly, your program sends requests to the platform’s endpoint, authenticates with an API key, and gets billed for the actual number of tokens consumed.
The API is not subject to the interaction counts imposed by web or mobile chat UIs, but it still has its own constraints. Platforms typically enforce the following:
- Rate limits: Requests per minute (RPM) and tokens per minute (TPM).
- Concurrency limits: The number of simultaneous requests allowed.
- Account quotas: Some platforms set daily or monthly caps for new accounts or specific models.
- Context length: The total number of input plus output tokens per request must fit within the model’s context window.
Having sufficient balance only means you can continue paying; it doesn’t mean unlimited calls. Always check the platform’s rate-limit and billing documentation, as these policies change from time to time.
2. Getting Started with the API: From Registration to Key Management
- Registration and verification: Log in to the developer platform using your phone number or a third-party account, and complete the required identity verification process.
- Account top-up: LLM APIs are typically prepaid. Once your balance runs out, requests are rejected. Before adding funds, go to the billing or cost center to set up spending alerts and a monthly budget cap. For example, set an alert at 80% of your budget and a hard cap at the maximum amount you are willing to spend this month. Exact UI labels vary by platform.
- Create an API key: Under “API Keys” or “Key Management,” create a new key. Give it a descriptive name such as “thesis-writer-assistant” to make it easier to manage and revoke later.
- Review the documentation: Confirm the request endpoint, model name, request parameters, response format, error codes, and pricing table.
Key security is equally important:
- Store keys in environment variables or a key management service, never in code.
- Do not commit keys to Git repositories, screenshots, or chat windows.
- Use a separate key for each project so you can revoke and track costs independently.
- Rotate keys periodically per your team’s policy, e.g., every 90 days. Also grant each key only the minimum permissions required.
- If you suspect a leak, delete or reset the key in the console immediately and update your local configuration.
3. Choosing a Model and Estimating Token Costs
When selecting a model, compare four dimensions:
- Context window: The total length of input plus output the model can handle. Projects with extended conversations require a larger window.
- Price: Input and output token rates, which often differ.
- Rate limits: Whether the RPM and TPM limits meet your concurrency needs.
- Capability: Actual performance on writing, coding, reasoning, and other tasks. Test a few examples with your own workload.
A token is the basic unit of text processing for a model. It is not exactly the same as a word or character. In Chinese, one character typically maps to one or two tokens; in English, roughly four characters map to one token. Use the platform’s counting tool for estimates, but rely on the usage field in the response for final billing.
The formula for a single call is:
Single-call cost = (Input Tokens ÷ 1000 × Input Price per 1K tokens) + (Output Tokens ÷ 1000 × Output Price per 1K tokens)
For example, suppose a model charges ¥0.002 per 1K input tokens and ¥0.008 per 1K output tokens. A request with 3,000 input tokens and 1,000 output tokens would cost 3 × 0.002 + 1 × 0.008 = ¥0.014. These are sample prices only; always refer to the official pricing sheet.
Because every turn in a multi-turn conversation resends the full history, the input token count grows with conversation length. Periodically compressing the history and keeping only essential details can significantly reduce costs.
4. Migrating Historical Conversations: Sanitize First, Then Restructure
Existing long conversations often contain personal information, project secrets, and business data. Before migrating, complete the following steps:
- Export the conversation: Export the chat log from the original platform, or manually copy the content you need. Store the exported file in a local encrypted directory.
- Sanitize the data: Remove personal information such as names, phone numbers, emails, ID numbers, and addresses. Remove API keys, passwords, and tokens. Replace company names, client names, and undisclosed data with placeholders.
- Trim the content: Keep the goal, constraints, key conclusions, and recent important turns. Remove lengthy pleasantries and abandoned approaches, or condense them into a short summary.
- Restructure into the messages format: Use
systemfor the system instruction,userfor your input, andassistantfor the model’s reply. Example:
[{"role": "system", "content": "You are a technical writing assistant. Keep answers concise."},{"role": "user", "content": "Project background: ... (sanitized summary)"},{"role": "assistant", "content": "Got it. I understand the project background."},{"role": "user", "content": "Please continue drafting Chapter 3 now."}]
- Confirm authorization: If the historical conversation contains information or materials belonging to others, obtain their consent first. Also review the platform’s data usage and privacy policy to understand whether your data is used for model training and whether you can opt out.
After migration, delete local temporary export files and clear any sensitive content that may linger in terminal history.
5. Setting Environment Variables and Making Your First Call
On Linux or macOS, run the following in your terminal:
export LLM_API_KEY="your_api_key"
To make it persistent across reboots, add that line to ~/.bashrc or ~/.zshrc, then run source to reload.
In Windows PowerShell, use:
$env:LLM_API_KEY="your_api_key"
The PowerShell setting applies only to the current window. For a permanent setting, add the variable via the System Environment Variables dialog.
Here is a minimal working request example. The endpoint and model name are placeholders; replace them with the real values from the platform documentation:
curl https://api.example.com/v1/chat/completions \-H "Content-Type: application/json" \-H "Authorization: Bearer $LLM_API_KEY" \-d '{"model": "your-model-name","messages": [{"role": "system", "content": "You are a patient technical assistant."},{"role": "user", "content": "Explain what a token is in three sentences."}],"max_tokens": 300}'
The response is typically in JSON format. The reply content is in the choices[0].message.content field, and token usage is in the usage field. Your program should read both. On error, log the error code and request_id; do not print the full response body directly.
6. Troubleshooting Common Error Codes
| Error Code | Common Causes | How to Fix |
|---|---|---|
| 400 | Invalid parameters, e.g., wrong model name, malformed messages format, or context window exceeded |
Validate fields against the docs, trim history, and retry |
| 401 | API key missing, incorrect, or deleted | Check the environment variable; create a new key if needed |
| 403 | Account not verified, no access to the model, or project disabled | Confirm identity verification and model permissions in the console |
| 429 | Rate limit, concurrency limit, or quota exhausted | Retry with exponential backoff, lower concurrency, or check billing and quotas |
| 500 or 503 | Temporary server-side failure on the platform | Retry after a delay and set a maximum number of retries to avoid infinite loops |
| Insufficient balance | Prepaid balance has run out | Top up and retry; confirm spending alerts are active |
When troubleshooting, record the error code, the request_id from the response, the request time, and the model name. Sanitize logs before saving or sharing.
7. Pre-Launch Checklist
Before fully moving your conversations to the API, verify the following:
- Account balance is sufficient; spending alerts and budget caps are configured.
- API keys are in environment variables and a dedicated key exists for this project.
- The selected model’s context window can accommodate your current history and expected output.
- Historical conversations have been sanitized of personal and business data.
- Logs and monitoring contain no plain-text keys or personal information.
- Retry and backoff strategies are configured for errors like 429 and 500.
