First Steps in AI Engineering: Building a Simple CLI Chatbot
Описание и применение
Минимальный CLI‑чат‑бот на Python с использованием Anthropic Claude, ограничением токенов и сохранением истории диалога.
Задача
Build a lightweight, turn‑based chatbot without heavy frameworks, while managing token usage and ensuring deterministic greetings.
Результат
A minimal, cost‑aware CLI chatbot that can converse with Claude‑Sonnet‑5, print responses, and maintain conversation history.
Рабочий процесс
- Install Anthropic Python library
- Import os and Anthropic
- Set ANTHROPIC_API_KEY in environment
- Define SYSTEM_PROMPT and STARTING_MESSAGE
- Prompt user for input
- Create messages list with assistant greeting and user question
- Instantiate Anthropic client with api_key
- Call client.messages.create with system, messages, model, max_tokens
- Iterate over response.content blocks to extract text
- Print assistant response
- Append assistant response to messages list for next turn
Доказательства
I wanted to organise myself by placing the constants in one area, and I knew I needed to starting directive for the client/chatbot to know the whole context
response = client.messages.create(max_tokens=1024, system=SYSTEM_PROMPT, messages=messages, model="claude-sonnet-5")
- Дата записи: 2026-09-20T10:15:33.269Z