★ Final Challenge

Run the Whole Conversation

50 pts

Your chatbot is done: it starts from a system prompt, loops through turns, remembers the conversation, trims to a budget, and survives flaky calls. The final challenge is the engine of that app as one function — everything you built, with the interactive input() loop replaced by a list of scripted user inputs and the real API replaced by an injected client. It doesn't mutate user_inputs or return anything shared, so with a deterministic fake client (as in the tests) it behaves like a pure function; wire in the real anthropic_client and it's exactly your app's loop, network calls included.

Implement run_conversation(user_inputs, client, system_prompt=None, max_messages=None):

  • Start the history the way make_conversation does: empty if system_prompt is None/blank, otherwise one system message with the prompt stripped.
  • For each string in user_inputs, in order, run one turn the way chat_turn does: append the user message, call client(history) with the full current history, append the assistant reply.
  • After each turn, if max_messages is not None, trim the history the way trim_history does: keep a leading system message (if present) plus the most recent messages, at most max_messages total. You may assume max_messages >= 3 when provided.
  • Return the final history list.

Log in to submit a solution and earn points.