Skip to content

< CurriculumContext engineering · 23 of 46 ·15 · Level 05, Agents

Ask Five Times, Keep the Majority

medium · implement · LLM Fundamentals

Ask a model the same question twice and you can get two different answers. The cheap fix: ask several times and keep the answer that comes back most often.

Implement consistent_answer(llm, question, samples=5):

  1. Call llm.ask(question) exactly samples times, same question each time.
  2. Tidy each reply before counting: strip whitespace, lowercase it, drop a trailing period. "144.", " 144" and "144" all count as "144".
  3. Return the tidied reply that appeared most often. On a tie, any tied answer will do.

The catch: skip step 2 and every count is 1, and "most common" quietly becomes "whichever came first". Return the tidied text: "Paris." gives you "paris".