In this recipe you build a small customer support chatbot that runs in your terminal. It remembers the conversation, streams its replies as they are written, falls back to a second model if the first one fails, and groups every message of a conversation under one trace so you can read it back in the dashboard.
What you need
- A Spro API key. See API keys.
- Python 3.9 or newer, and the OpenAI SDK:
pip install openai.
Set your key as an environment variable:
export SPRO_API_KEY="YOUR_SPRO_API_KEY"The code
Save this as support_bot.py.
import osimport uuid
from openai import OpenAI, PermissionDeniedError
client = OpenAI( base_url="https://spro.hridaai.com/v1", api_key=os.environ["SPRO_API_KEY"],)
SYSTEM_PROMPT = ( "You are a friendly support assistant for Acme, an online store. " "Keep answers short. If you don't know something, say so and offer to " "pass the question to a human.")
# One id for the whole conversation. Spro groups every request that carries it into one trace.conversation_id = f"support-{uuid.uuid4().hex[:8]}"
history = [{"role": "system", "content": SYSTEM_PROMPT}]print("Acme support. Type 'exit' to quit.\n")
while True: user_text = input("You: ").strip() if user_text.lower() in {"exit", "quit"}: break if not user_text: continue
history.append({"role": "user", "content": user_text})
try: stream = client.chat.completions.create( model="gpt-oss-120b", messages=history, stream=True, # If the first model fails, Spro tries these next, in order. extra_body={"models": ["gpt-oss-20b"]}, extra_headers={ "x-spro-trace-id": conversation_id, "x-spro-trace-name": "support-chatbot", }, )
print("Bot: ", end="", flush=True) reply = "" for chunk in stream: if chunk.choices and chunk.choices[0].delta.content: piece = chunk.choices[0].delta.content reply += piece print(piece, end="", flush=True) print("\n")
history.append({"role": "assistant", "content": reply})
except PermissionDeniedError: # A guardrail blocked the message (HTTP 403). Drop it so it isn't resent next turn. history.pop() print("Bot: Sorry, I can't help with that message.\n")Run it
python support_bot.pyYou: Where is my order?Bot: I can help with that. Could you share your order number?How it works
- Memory. The chatbot keeps every message in
historyand sends it with each request. The model has no memory of its own, so it only knows what you send. - Streaming.
stream=Truereturns the reply in pieces, and the loop prints each piece as it arrives. See Streaming. - Fallback.
modelis tried first, then each entry inmodels. See Routing strategies. - One trace per conversation. Every request carries the same
x-spro-trace-id, so they are grouped together. Open Traces in your workspace and look forsupport-chatbot. See Tracing. - Blocked messages. If an input guardrail blocks a message, Spro returns a
403, which the SDK raises asPermissionDeniedError. The code removes that message from the history so it isn't sent again on the next turn.
Make it safer
Customers paste things like email addresses and card numbers into a support chat. Turn on the Sensitive Information Disclosure guardrail with the redact action, and Spro masks those values before they reach the model provider. You change nothing in this code. See Guardrails and Zero Data Retention.
Ideas to extend it
- Give the bot tools to look up orders. See Build an order-status agent.
- Keep only the last few turns in
history, so long conversations stay cheap.
