OpenAI Python Chatbot: Build a Terminal Chat Loop
An OpenAI Python chatbot in the terminal takes three things: the OpenAI SDK, a base URL pointed at the gateway, and one list that holds conversation history. No framework needed: one Python file, one API key, and a terminal is enough for a back and forth chatbot.
A terminal chatbot in Python only needs the OpenAI SDK, the base URL https://api.tokenomixai.com/v1, and one conversation history list you manage yourself.
What you need for an OpenAI Python chatbot
A terminal chatbot needs a handful of parts, and you can put all of them in one empty folder. No database, no framework, no extra service.
- Python installed and callable from your terminal
- The openai package for Python, installed with pip
- A TokenomixAI API key in the sk-tkx-... format
- The base URL https://api.tokenomixai.com/v1
- One model from the model list, stored as a MODEL_ID constant
You get the key after buying a package on the website. No account and no credit card: the key arrives after payment by QRIS or USDT. Keep it in an environment variable or a separate file you never commit to Git.
Step one: install the SDK and send a chat completion
The first call usually looks harder than it is: one client object, one messages array, one model name. If you leave the base URL out, the SDK hits its default endpoint and your sk-tkx- key is rejected right away.
from openai import OpenAI
client = OpenAI(base_url="https://api.tokenomixai.com/v1", api_key="sk-tkx-...")
reply = client.chat.completions.create(
model="MODEL_ID", # salin ID model dari tokenomixai.com/docs
messages=[{"role": "user", "content": "Halo"}],
)
print(reply.choices[0].message.content)Three things matter in that snippet: base_url points at the gateway, api_key is read from an environment variable, and the model name comes from the list on the docs site. Once you get a reply, wrap the call in a while loop that reads terminal input and test it with a short question.
The right base URL for the OpenAI SDK, and why Claude Code differs
The OpenAI SDK and other OpenAI-compatible clients need the full base URL including /v1, which is https://api.tokenomixai.com/v1. The SDK appends the chat completions path itself, so /v1 belongs in the base URL value, not in your call code.
Claude Code is the exception. There, ANTHROPIC_BASE_URL is https://api.tokenomixai.com without /v1, because Claude Code adds /v1/messages itself. If you run both tools on one machine, keep the two values separate so you don't mix them up while debugging.
A Python script needs only one of them. A 404 that says the path was not found almost always means the base URL has a missing or an extra /v1. The meaning of the key format and the base URL is covered in OpenAI compatible API key.
How to keep conversation history so the chatbot remembers context
The model has no memory between requests. Context exists only because every request resends the whole conversation, so the history lives on the Python side and you own it.
- Create the messages list once when the program starts
- Add a system message in the first position to set the answer style
- Append each user input as a user message
- Append the model reply as an assistant message before the next request
The consequence is that a longer conversation sends more tokens on every turn. If the quota drains faster than you expected, trim the history: keep the system message and the last few turns, then drop the oldest ones.
Don't share one history list between two different users. If the chatbot serves several people, keep one list per session so answers don't carry context from someone else.
Check quota and rate limits before other people use the chatbot
TokenomixAI counts quota in tokens, not in time windows, so there is no five hour limit. The key keeps working while tokens remain and before it expires. You can check the remaining balance at any time with the quota endpoint.
curl https://api.tokenomixai.com/v1/quota -H "Authorization: Bearer sk-tkx-..."The response includes status, remainingTokens, maxTokens, usagePercent, expiresAt, and low, which is true at 10 percent or less. Print a warning in the terminal when low turns true, so you aren't caught mid-test.
There is also a rate limit of 600 requests per minute per account, shared by all keys on that account. Above that the API returns 429 with a Retry-After header, so an aggressive test loop can hit it even while plenty of tokens remain.
Common errors when building a chatbot with the OpenAI SDK
| Error | Cause and what to check |
|---|---|
| 401 Unauthorized | The key is wrong, truncated, or not read from the environment variable. Check the sk-tkx- format and make sure no trailing space was copied. |
| 404 Not Found | The base URL has a missing or extra /v1, or the model name isn't in the list. Check the model list on the docs site. |
| 429 Too Many Requests | You passed 600 requests per minute. Read Retry-After, wait, then slow the loop down. |
| status exhausted | The token quota is used up. Top up on the website; the same key works again once tokens arrive. |
If a 401 shows up with a freshly created key, compare it against 401 API key error. Other questions about keys, quota and the gateway are collected in the FAQ.
Frequently asked questions
Do I need a credit card to try this chatbot?
No. TokenomixAI doesn't ask for an account or a card: you buy a package on the website, pay with QRIS or USDT, and the API key arrives after payment. The key works right away in a Python script with the base URL https://api.tokenomixai.com/v1.
Why does conversation history burn through quota so fast?
Every request resends the whole history, so the token count includes all earlier messages. The longer the conversation, the more tokens per turn. Trim the history or start a new session when a topic is finished.
Are prompts and answers stored on the server?
Only request metadata is logged by the gateway: model, token count, time and status. Prompts and completions are not stored.
Can the chatbot take images as input?
The available models are text and code models. Some accept images as input (vision), but there is no image, video or audio generation. The model list on the docs site shows which ones accept image input.
What happens if quota runs out in the middle of testing?
Top up on the website. Remaining tokens are added together and validity follows the latest top-up. The key stays the same, so nothing in the script needs to change.