A minimal, professional command-line chatbot built in Python using OpenAI’s Chat Completions API. It evolves in three stages: from a single hardcoded prompt, to interactive I/O with token cost reporting, and finally to a persistent chat with function-called termination.
- Interactive CLI prompt (
Enter a message:) - Assistant responses printed as
Assistant: ... - Token usage cost per interaction (
Cost: $...) - Continuous conversation loop
- Function calling to terminate the program on request (prints the call ID)
- Hardcoded user message: “What are you?”
- Sends a system + user message to the API, prints the assistant reply once.
- Useful to validate environment and SDK integration.
- Prompts the user:
Enter a message: - Prints both:
You: <message>Assistant: <reply>
- Computes and prints total token cost per interaction (input + output).
- Pricing (USD per 1M tokens) is configurable via env vars:
OPENAI_INPUT_COST_PER_MTOK(default0.15)OPENAI_OUTPUT_COST_PER_MTOK(default0.60)
- Continuous loop: after each turn, the program returns to the prompt.
- Declares a tool/function
end_conversationfor the model. - If the user asks to stop (e.g., “End conversation”), the assistant calls the function:
- The program prints the function call ID on its own line,
- Prints
Assistant: None, - Prints the Cost,
- Exits with code
0.
.
├─ main.py # CLI app (stages implemented here)
├─ .env # Environment variables (not committed)
└─ README.md # This file
- Python & Dependencies
python -m venv .venv
# Windows: .venv\Scripts\activate
# macOS/Linux: source .venv/bin/activate
pip install --upgrade openai python-dotenv- Environment
Create a
.envfile next tomain.py:
OPENAI_API_KEY=sk-... # your key
OPENAI_MODEL=gpt-4o-mini # optional, default used if omitted
OPENAI_INPUT_COST_PER_MTOK=0.15 # optional
OPENAI_OUTPUT_COST_PER_MTOK=0.60 # optional
Do not commit
.env.
python main.pyExamples (Stage 2 & 3 format):
Enter a message: > What is 5 + 10?
You: What is 5 + 10?
Assistant: 5 + 10 equals 15.
Cost: $0.00003000
When ending the chat (Stage 3):
Enter a message: > End conversation
call_TjO2fMKrLs6u... <-- function call ID
You: End conversation
Assistant: None
Cost: $0.00006350
-
Environment loading:
python-dotenvloads.envusing a path tied tomain.py, ensuring it works even if the current working directory changes. -
Messages format: Uses the Chat Completions
messages=[{role, content}, ...]with a concisesystemprompt to guide behavior. -
Cost calculation:
def compute_cost(prompt_tokens, completion_tokens): in_cost = (prompt_tokens / 1_000_000) * INPUT_COST_PER_MTOK out_cost = (completion_tokens / 1_000_000) * OUTPUT_COST_PER_MTOK return in_cost + out_cost
Reads token counts from
resp.usage. -
Function calling (termination):
tools = [{ "type": "function", "function": { "name": "end_conversation", "description": "Terminate the program immediately.", "parameters": {"type": "object", "properties": {}, "required": []}, }, }]
If
resp.choices[0].message.tool_callsis present, the program prints the tool call ID, showsAssistant: None, the Cost, and exits.
- No sensitive data is logged by default.
- Keep your API key only in
.envand never commit it. - Review logs/prints before sharing console output.
You get a compact, production-ready CLI chatbot that:
- Works for one-shot prompts,
- Supports interactive conversations with cost visibility, and
- Terminates cleanly via assistant-initiated function calling.
Enjoy building on top of it! 🚀