Quickstart chat app
Build a terminal chat that remembers the conversation, streams each answer and adds up its charge.
- Start from the Quickstart: the key in your environment, the library installed.
- Keep the conversation.
- Stream each answer.
- Write the app.
- Run it and read what it cost.
1. Start from the Quickstart
The app picks up where the Quickstart ends: an API keyAPI keyThe key you create and send with every request that calls a model.More on API keys → in OPENDUNES_API_KEY, the OpenAI library installed, one request sent. Do those five steps first, then come back.
| Value | Set to |
|---|---|
| Base URL | https://opendunes.com/v1 |
| API key | OPENDUNES_API_KEY, in the terminal you run the app from |
| Model | anthropic/claude-sonnet-4.5 |
2. Keep the conversation
The model remembers nothing between two requests. The app keeps the conversation in a list and sends all of it on every turn.
| Role | Holds | Added |
|---|---|---|
system | how the model should behave | once, at the top |
user | the question typed | before each request |
assistant | the model's answer | after each answer |
Don't send only the new question. The model loses the thread, and "And on Friday?" means nothing to it.
Do add each answer to the list as an assistant turn before the next question.
Every turn sends the whole conversation again, so each turn reads more tokens than the one before, and costs more. For a long chat, drop the oldest turns and keep the system line.
3. Stream each answer
With "stream": true, the answer arrives in pieces and the app prints each piece as it comes. The last piece carries no words: it carries usage.cost, the charge for that turn in DADAThe Algerian dinar, the one currency the docs show.More on Balance and charges →. Streaming shows every event.
A streamed answer carries no X-Balance-Available header. The turn's charge is in the closing event, and the app adds it to the chat's total.
4. Write the app
An empty line or exit ends the chat. The cURL tab sends one turn from inside a conversation: the history the app would send on its second question.
import os
from openai import OpenAI
API_KEY = os.environ["OPENDUNES_API_KEY"]
BASE_URL = "https://opendunes.com/v1"
MODEL = "anthropic/claude-sonnet-4.5"
SYSTEM = "You help a shop in Oran answer its customers. Be brief."
client = OpenAI(base_url=BASE_URL, api_key=API_KEY)
history = [{"role": "system", "content": SYSTEM}]
spent = 0.0
while True:
question = input("You: ").strip()
if question in ("", "exit"):
break
history.append({"role": "user", "content": question})
stream = client.chat.completions.create(
model=MODEL,
messages=history,
stream=True,
)
answer = ""
print("Answer: ", end="", flush=True)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
words = chunk.choices[0].delta.content
answer += words
print(words, end="", flush=True)
if chunk.usage:
spent += chunk.usage.cost
print()
history.append({"role": "assistant", "content": answer})
print(f"This chat cost {round(spent, 6)} DA")5. Run it and read what it cost
Save the Python tab as chat.py and run it. Each tab runs the same way in its own language: node chat.mjs, go run chat.go.
Illustrative: the answers and the charge are an example.
$ python chat.py
You: Do you deliver to Bir El Djir?
Answer: Yes, within the day. Order before 16:00 for an evening drop.
You: And on Friday?
Answer: On Friday we deliver after the midday prayer, until 20:00.
You: exit
This chat cost 0.412776 DAThe total is the sum of each turn's usage.cost. Each turn is also a line in Activity, in the app, with its tokens and its charge. How a request is charged: Balance and charges.
At zero or below, or when the balanceBalanceWhat you can spend, in DA.More on Balance and charges → cannot pay the request's hold, the next turn gets a 402 and the app stops with an error. New requests are refused until a depositDepositMoney entering your balance, by CIB or Edahabia card.More on Balance and charges → covers what the balance owes: see Errors and retries.
Next: Move to OpenDunes · Text · Tools
Last modified on