统一网关入口:所有脚本只需指向这一个地址
·ThisToken.AI·
Tutorials入门教程ThisToken.AI
client = OpenAI(
api_key=os.environ["THISTOKEN_API_KEY"],
base_url="https://api.thistoken.ai/v1",
)
resp = client.chat.completions.create(
model="gpt-4o-mini", # 网关支持的具体模型以控制台列表为准
messages=[
{"role": "system", "content": "你是一个简洁的助手。"},
{"role": "user", "content": "用一句话说明什么是API网关。"},
],
)
print(resp.choices[0].message.content)
Note that the Key is not hardcoded here but read from an environment variable—this is a rule that should be established from day one.
## IV. Execution Steps for Batch Migration
After verification succeeds, proceed in the following order, with each step having clear completion criteria:
**Step 1: Establish a configuration center.** Create a unified `llm_client.py` module from which all scripts import the client. Later, if you need to change the gateway address or switch Keys, you only change it in one place.
llm_client.py —— 全团队共用的客户端工厂
import os
from openai import OpenAI
def get_client() -> OpenAI:
return OpenAI(
api_key=os.environ["THISTOKEN_API_KEY"],
base_url="https://api.thistoken.ai/v1",
)
**Step 2: Replace script by script.** Following the inventory table order, replace the client initialization in each script with `from llm_client import get_client`. After replacement, run a smoke test of that script and check it off on the inventory table. It's recommended to make one commit per script for easy rollback.
**Step 3: Revoke old Keys.** Once a script's migration is confirmed complete, immediately deactivate the corresponding old Key on the original provider's platform. Don't "keep it just in case"—keeping it is a risk exposure. This step should be a mandatory item on the checklist, not a verbal reminder.
**Step 4: Establish access rules for new scripts.** From now on, whenever anyone writes a new script, Keys must be obtained from the team's key management (even if it's just an encrypted shared password vault), and base_url must always point to the gateway. List this as a check item during code review.
## V. Risk Control Checklist
Here are the common pitfalls during the transformation, shared with the team in advance:
- **Don't switch everything at once.** Roll out gradually—switch low-risk scripts first, observe for a few days, then switch core business.
- **Preserve rollback capability.** Before deactivating old Keys, keep at least one encrypted backup in a key management tool for emergency rollback. But backups must have a destruction deadline.
- **Verify model parameters.** After switching gateways, model names may need adjustment. If the style or length of output differs from before, check the parameters first before suspecting the gateway.
- **Keep bill attribution up to date.** Use the gateway's usage statistics capabilities to distinguish projects and people by Key, replacing past manual reconciliation.
- **Least privilege.** Not every script needs the same Key. Issue Keys per project, so that if a Key is leaked, the impact is contained.
## VI. What the Team Gains After the Transformation
At the code level, the changes may total fewer than fifty lines; but at the process level, what you gain is: a unified endpoint, an auditable call log, a permission system that can be revoked at any time, and an onboarding standard that new team members can understand in five minutes.
For independent developers and small teams, this kind of "invisible infrastructure cleanup" is the easiest to keep postponing—but the later you do it, the more scattered Keys accumulate and the higher the recovery cost. An afternoon's investment buys you composure during every future personnel change and provider adjustment.
If you're planning to start this consolidation, the first step is to register a gateway account and create your first Key: https://api.thistoken.ai/register
---
Every example in this post runs with a single API key — get yours at https://api.thistoken.ai/register and start in minutes.Хотите попробовать Token.AI?
Создайте API Key уровня проекта, включите каналы в консоли и настройте маршрутизацию, бюджеты и журналы аудита.
注册 ThisToken.AI 并获取 API Key