Building a Controllable AI Chat Service with Open-Source Frontend and APIs
As the cost of large language model (LLM) inference remains high, many platforms are tightening their free usage limits. This article explores an alternative approach: combining vendor API services with open-source chat frontends to build a controllable AI chat application. It covers obtaining API keys, local execution, cloud static hosting, setting up a server-side proxy, synchronizing multi-device chat records, and troubleshooting common issues, providing a practical guide for developers and teams.
In recent years, the capabilities of large language models (LLMs) have rapidly improved, drawing an increasing number of users to various AI chat platforms. At the same time, the cost of inference compute remains high. To control expenses, some providers have tightened free usage quotas, and a few have even suspended personal subscription recharges. For users who have long relied on free services, deep conversations, role-playing, and long-text processing may be affected.
This article discusses an alternative solution: purchasing a vendor’s API service and pairing it with an open-source chat frontend to build your own chat application. This approach may not necessarily be cheaper, but in scenarios requiring controllable usage and autonomous configuration, it is typically more flexible than relying solely on web interfaces.
1. Why Free Quotas Are Tightening
LLM inference requires continuously occupying expensive GPU resources. With a large number of free users and long single conversations, the compute costs borne by the platform increase accordingly. Providers generally cite compute pressure as the main reason for quotas, which is a common view in the industry.
The pay-as-you-go API model directly links costs to usage. Providers usually charge based on the number of tokens, and users pay for actual consumption. For developers and teams requiring long-term use of models, this billing method is more transparent and facilitates budget planning.
2. Combining Open-Source Frontend with APIs
Some providers open-source chat frontend projects similar to their web interfaces. After users apply for an API key on the open platform and top up their balance, they can simply enter the key into the frontend’s settings to use the same model’s chat capabilities. Some frontend projects also support importing exported historical conversations.
Before using it, a few things need to be confirmed: the specific name and version of the project, the API request address (Base URL), the available model names, and the account’s permissions and balance. This information should be based on the official repository and open platform documentation. The operational steps in this article are general processes; specific parameters need to be adjusted according to the actual project.
3. Preparation: Obtaining API Keys
Step 1: Register an account on the LLM open platform. Some platforms require identity verification; without it, you may be unable to create keys or increase call limits.
Step 2: Go to the API management page in the console and create a new key. When creating, you can name it by purpose for easier differentiation and revocation later.
Step 3: Top up based on expected usage. It is recommended to deposit a small amount first, run through the entire process, and then increase the balance while checking the unit price and billing rules in the bill.
Keys are usually displayed only once and should be saved immediately. They should be stored in server-side environment variables or a key management service, and never written into code repositories, screenshots, or chat logs.
4. Local Execution
The Release page of open-source projects generally provides packaged compressed files. After decompression, many people directly double-click the HTML file inside. In some browsers, this file protocol access method will be restricted; the page may open, but sending messages will yield no response.
A more reliable approach is to start a local static server in the decompressed directory. For example, after installing Python, execute the following command:
python -m http.server 8080
Then visit http://localhost:8080 in your browser. Open the settings page, fill in the API Base URL, model name, and key in sequence, and send a test message. If you receive a reply, the link is basically normal.
The key filled in the browser will be saved in local storage. When using a shared computer, you should clear the settings after use, or switch to a server-side proxy. Local execution is suitable for personal testing and not for multiple people sharing the same key.
5. Cloud Static Hosting
If you want to access the service on both mobile phones and computers, you can deploy the frontend to the cloud. Developers execute the build command locally:
npm installnpm run build
After the build is complete, a dist directory will be generated. Upload this directory to object storage or the static website hosting feature of other cloud services. During deployment, pay attention to three settings: whether HTTPS is enabled, whether the cache policy will cause old versions of the page to persist for a long time, and whether the Cross-Origin Resource Sharing (CORS) configuration allows your domain to access the interface.
There is an important limitation here. The frontend code will be downloaded by the browser, and anyone who can open the page can see its contents in the developer tools. If you write the API key into the frontend, the key becomes public. Therefore, when deploying on the public internet, requests should be forwarded through a server-side proxy, and the key should only be stored on the server side.
6. Advanced: Setting Up a Server-Side Proxy
The role of a proxy gateway is to add a layer of control between the user and the LLM API. The frontend sends the request to a self-built service, which first verifies the user’s identity, and then forwards it to the model API carrying the key saved on the server side.
Below is a simplified example, used only to illustrate the concept, and is not suitable for direct use in a production environment:
// Example code, not ready for productionconst express = require('express');const app = express();app.use(express.json());app.post('/api/chat', async (req, res) => {// 1. Verify user identity (verifyUserToken needs to be implemented by yourself)if (!verifyUserToken(req.headers.authorization)) {return res.status(401).json({ error: 'Unauthorized' });}try {// 2. Request the model API using the key from server-side environment variablesconst data = await callLLMApi(process.env.LLM_API_KEY, req.body.prompt);res.json(data);} catch (err) {// 3. Error handling: log the error, but do not output the keyconsole.error('upstream error', err.message);res.status(502).json({ error: 'Upstream error' });}});app.listen(3000);
During actual deployment, the following content still needs to be added: request timeout and limited retries, rate limiting by user or IP, abnormal traffic alerts, and log masking. Logs must not contain keys or user privacy data.
7. Multi-Device Chat Record Synchronization
If you need to continue conversations across multiple devices, you can introduce a cloud database or object storage to modify the frontend’s data saving logic. After the conversation ends, serialize and upload the context; when logging in on a different device, fetch the history first.
Chat records often contain work content, personal information, and even trade secrets. Before launching the synchronization feature, several things should be confirmed: whether the transmission is encrypted, whether the storage is encrypted, which accounts can access the data, how long the history is retained, and whether users can delete their own records. If these issues are not handled properly, the synchronization feature will bring privacy risks.
8. Troubleshooting Common Issues
Browser reports a CORS error: This means the interface does not allow cross-origin access from the current domain. You can forward it through a server-side proxy, or add your site to the allowed domain list on the platform.
Returns 401: Usually caused by an incorrect key, a revoked key, or an incorrect request header format. Check if the key is complete and if there are any extra spaces before or after it.
Returns 429: The request frequency or concurrency exceeds the limit. Reduce the request frequency or apply for a quota increase.
Prompts that the model does not exist: The model name is misspelled, or the current account does not have permission for that model. Refer to the model list in the platform documentation.
Static resource 404: The build artifacts were not fully uploaded, or the routing configuration is incorrect. Confirm that the file structure of the
distdirectory matches the online version.Suspected key leakage: Immediately revoke the old key in the console, generate a new one, update the server-side environment variables, and then check the bills and call logs to confirm if there is any abnormal usage.
9. Conclusion
The tightening of free quotas indicates that the compute costs of LLM services must be borne by someone. Pay-as-you-go APIs link costs to usage, allowing users to pay as needed. Open-source frontends lower the barrier to setup, but once built, developers remain responsible for key protection, access control, and cost monitoring.
Individual users can start with local execution and API top-ups, suitable for light to moderate usage. Teams and scenarios requiring long-term operation are better suited for server-side proxies and cloud deployment, along with establishing basic monitoring. Both methods require some command-line and configuration knowledge. As long as you check each step carefully, you can achieve a controllable and maintainable chat service.
