Real World Example

A Command-Line ShopEase Assistant

Put it all together: a system prompt, conversation history, streaming and error handling. This is the first version of the ShopEase AI Assistant; later modules add documents, tools and a web UI.

import anthropic
from dotenv import load_dotenv

load_dotenv()
client = anthropic.Anthropic(max_retries=3)

SYSTEM = """You are the ShopEase support assistant for an Indian e-commerce store.
Keep answers under 80 words. Be warm and clear.
You cannot see orders yet: if asked about a specific order, ask the customer to share the order ID
and say a human agent will check it."""

history = []
print("ShopEase Assistant (type 'quit' to exit)")
while True:
    user = input("\nYou: ").strip()
    if user.lower() == "quit":
        break
    history.append({"role": "user", "content": user})
    try:
        print("Bot: ", end="")
        with client.messages.stream(model="claude-sonnet-5-5", max_tokens=400,
                                    system=SYSTEM, messages=history) as stream:
            for text in stream.text_stream:
                print(text, end="", flush=True)
            reply = stream.get_final_message()
        history.append({"role": "assistant", "content": reply.content})
    except anthropic.APIError as e:
        history.pop()                       # drop the unanswered user turn
        print(f"\n[Sorry, the assistant is unavailable right now: {e.__class__.__name__}]")

Sample session

python assistant.py

Example output

ShopEase Assistant (type 'quit' to exit)

You: Hi! Do you have COD?
Bot: Hi! Yes, ShopEase offers Cash on Delivery on most products for orders up to ₹10,000. You'll see the COD option at checkout if it's available for your pincode.

You: ok and my order SE-10423 is late
Bot: I'm sorry about the delay! I can't see order details myself, but I've noted your order ID SE-10423. A human agent will check its status and update you shortly.

You: quit

Notice

  • The COD details in the sample reply come from the model's guess, not from ShopEase data. That is a hallucination risk. In the medium level the assistant answers from real policy documents (RAG).
  • reply.content (the list of blocks) is stored in history, which keeps any thinking blocks intact for the next turn.