Building the Prompt Within a Budget

Number the retrieved chunks so the model can cite them, and stop adding chunks before the prompt gets too long.

AI · intermediate · 7 min · 33 steps · free

What you'll learn

prompt_assembly.py

def count_tokens(text):
    return len(text.split())

def build_prompt(question, chunks, budget):
    parts = []
    used = count_tokens(question)
    for i, chunk in enumerate(chunks, start=1):
        cost = count_tokens(chunk)
        if used + cost > budget:
            break
        parts.append(f'[{i}] {chunk}')
        used += cost
    context = '\n'.join(parts)
    return f'Answer only from these sources and cite them.\n{context}\nQuestion: {question}', used

chunks = ['refunds are free for 30 days', 'keep the receipt', 'shipping to india takes 5 days']
prompt, used = build_prompt('are refunds free', chunks, 12)
print(used)
print(prompt)

Step by step

  1. Python defines the function count_tokens(). Its body does not run yet: it runs each time the function is called.
  2. Python defines the function build_prompt(). Its body does not run yet: it runs each time the function is called.
  3. chunks is created and points to a new list: ['refunds are free for 30 days', 'keep the receipt', 'shipping to india takes 5 days'].
  4. Python calls build_prompt(), with question = 'are refunds free', chunks = ['refunds are free for 30 days', 'keep the receipt', 'shipping to india takes 5 days'] and budget = 12. It gets its own names.
  5. parts is created and points to a new list: [].
  6. Python calls count_tokens(), with text = 'are refunds free'. It gets its own names.
  7. The function returns len(text.split()).
  8. The question uses 3 of the 12-token budget.
  9. i is created with the value 1. chunk is created with the value 'refunds are free for 30 days'.
  10. Python calls count_tokens(), with text = 'refunds are free for 30 days'. It gets its own names.
  11. The function returns len(text.split()).
  12. count_tokens() has finished, so its names are gone. The value it returned, 6, is stored in cost.
  13. used + cost > budget is false, so the block under it is skipped.
  14. Chunk 1 fits: it goes in as "[1] …" so the answer can point back to it.
  15. used changes from 3 to 9.
  16. i changes from 1 to 2. chunk changes from 'refunds are free for 30 days' to 'keep the receipt'.
  17. Python calls count_tokens(), with text = 'keep the receipt'. It gets its own names.
  18. The function returns len(text.split()).
  19. count_tokens() has finished, so its names are gone. The value it returned, 3, is stored in cost.
  20. used + cost > budget is false, so the block under it is skipped.
  21. '[2] keep the receipt' is added to the end of the list that parts points to.
  22. used changes from 9 to 12.
  23. i changes from 2 to 3. chunk changes from 'keep the receipt' to 'shipping to india takes 5 days'.
  24. Python calls count_tokens(), with text = 'shipping to india takes 5 days'. It gets its own names.
  25. The function returns len(text.split()).
  26. count_tokens() has finished, so its names are gone. The value it returned, 6, is stored in cost.
  27. 12 + 6 would be 18, over the budget of 12.
  28. Line runs: break.
  29. Two numbered sources make the context.
  30. The function returns f'Answer only from these sources and cite them.\n{context}\nQuestion: {question}', used.
  31. build_prompt() has finished, so its names are gone. The value it returned, 'Answer only from these sources and cite them.\n[1] refunds are free for 30 days\n[2] keep the receipt\nQuestion: are refunds free', is stored in prompt. used is created with the value 12.
  32. It prints 12.
  33. The final prompt: an instruction, numbered sources, then the question.

Common mistakes

Best practices

Key takeaways

Check yourself

Why number the chunks?

Answer: So the answer can cite [1], [2]

Too much context can?

Answer: Bury the useful part and cost more

·

More in The RAG Pipeline