Where history lives
With Chat Completions, your application sends messages on each turn. The model endpoint is not required to remember a previous request by API key. A deployed bot stores history by account and conversation ID and checks that the user may access it.
Never share one global message array across visitors. Authenticate, load the authorized conversation, then request a model response. Attachments and quoted documents are data, not new administrative instructions.
A two-turn test
Install the openai Python package and set CLODEX_API_KEY and CLODEX_MODEL. The example makes two billable requests: it gives a word, then asks for it. The expected word cedar is a fixture condition, not a recorded model output.
candidate stays local until the answer completes. A failure adds neither partial output nor a duplicate user message to the committed history. A real persistence layer needs an equivalent atomic update.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["CLODEX_API_KEY"],
base_url="https://clodex.xyz/v1",
timeout=60.0,
max_retries=0,
)
# One isolated test conversation, not shared between users.
history = [{"role": "system", "content": "Answer briefly. Do not use tools."}]
for user_text in ["Remember the word cedar.", "Which word did I give you?"]:
candidate = history + [{"role": "user", "content": user_text}]
result = client.chat.completions.create(
model=os.environ["CLODEX_MODEL"], messages=candidate, stream=False,
)
if not result.choices:
raise RuntimeError("Missing choices")
choice = result.choices[0]
if choice.finish_reason != "stop" or not choice.message.content.strip():
raise RuntimeError(f"Incomplete text turn: {choice.finish_reason}")
history = candidate + [{"role": "assistant", "content": choice.message.content}]
print(choice.message.content)
What a Telegram bot adds
Use Telegram update_id and your conversation identifier to deduplicate incoming events. Sending a Telegram message is a separate action after model completion. Reprocessing one update must not send duplicate replies or repeat external operations.
The Telegram token and CLODEx key are different secrets. Keep both server-side and record only safe identifiers in errors. For group chats, explicitly decide whether context is shared and who may clear it.
Long conversations and interrupted streams
Conversation history consumes input tokens again. Bound it by token size and meaningful boundaries; do not separate a tool result from its call. A summary of older messages is new data and may itself be wrong.
For streaming, distinguish provisional display from a committed answer. A disconnected stream is not completion. If you add tools, validate arguments and permissions server-side; a suggested tool call alone cannot authorize a refund or deletion.
Documentation and example scope
Connection settings follow CLODEx documentation. Examples target the stated protocol; they are not recorded inference results and do not promise identical features across models. A successful generation request can incur a charge.
Frequently asked questions
Is this a production multi-user bot?
No. It is a reproducible single-conversation example. Your application supplies persistence, authorization, deduplication and limits.
May I save a length-truncated answer?
You may retain it as an incomplete draft, but not as a completed answer. This example deliberately rejects it.
Can I use Responses instead?
If your model and client support it, yes. Implement history and result handling for that protocol rather than copying messages and choices literally.