统一入口:所有模型调用都走这里
client = OpenAI(
api_key=os.environ["THISTOKEN_API_KEY"],
base_url="https://api.thistoken.ai/v1",
)
USAGE_LOG = "ai_usage.jsonl"
def call_and_track(project: str, prompt: str, model: str = "gpt-4o-mini"):
resp = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
)
从响应中提取官方返回的用量数据,杜绝手工估算
usage = resp.usage
record = {
"ts": time.strftime("%Y-%m-%d %H:%M:%S"),
"project": project, # 业务维度:哪个项目在用
"model": model,
"prompt_tokens": usage.prompt_tokens,
"completion_tokens": usage.completion_tokens,
"total_tokens": usage.total_tokens,
}
追加写入本地台账,一行一条,格式统一
with open(USAGE_LOG, "a", encoding="utf-8") as f:
f.write(json.dumps(record, ensure_ascii=False) + "\n")
return resp.choices[0].message.content
if __name__ == "__main__":
answer = call_and_track(
project="demo",
prompt="用一句话解释什么是API网关。",
)
print(answer)
print("用量已记录到", USAGE_LOG)
Run it:
python usage_demo.py
If you see the model's reply and a new record with token counts appears in `ai_usage.jsonl`—congratulations, you've avoided the three failure patterns mentioned at the beginning: the data is recorded automatically by code, the metrics come from the `usage` field in the provider's response, and the storage is in your own hands.
## Step 3: From Ledger to Reports
With a uniformly formatted JSONL file, subsequent analysis is easy. Want to see each project's share of token consumption? Ten lines of code with `pandas` produces a table. Want to aggregate by day and plot trends? Periodically import the JSONL into any BI tool. When your team grows, swap file writes for a database or time-series storage—the function signature doesn't change, only the few ledger-writing lines inside `call_and_track`. That's the benefit of "tracking as a standardized pipeline": the upgrade cost is confined to a single point.
## A Few Practical Reminders
- **Route all call sites through `call_and_track`.** If someone calls `client.chat.completions.create` directly elsewhere, tracking will have gaps. Small teams should add this as a checklist item in code reviews.
- **Record failed requests too.** In production, add a try/except at the outer layer and write errors into the ledger as well (which layer should handle retries and fallback is also worth thinking through)—that's what makes tracking complete.
- **Reconcile regularly.** Once a month, compare the total token count in your local ledger against the vendor's bill. Large discrepancies usually mean some call sites aren't routed through the pipeline, or the Key has been leaked and misused.
## Final Thoughts
The earlier you set up usage tracking, the cheaper it is—wait another month, and that's another month of unaccounted-for usage muddle. The solution in this article—register an account, get a Key, run one piece of code—can be up and running in a single afternoon.
If you don't have an account yet, go register one now: https://api.thistoken.ai/register. Record your first usage data entry, starting today.
---
Tired of juggling provider integrations? Register at https://api.thistoken.ai/register and call every model through one base_url.Vous voulez essayer Token.AI ?
Créez une API Key au niveau du projet, activez les canaux dans la console et configurez le routage, les budgets et les journaux d'audit.
注册 ThisToken.AI 并获取 API Key