What goes into a prompt?
6 min read
The model only knows what is in front of it
A language model keeps nothing between calls. Each time your program calls it, the model starts fresh and reads one block of text. Whatever is in that block is all it knows about your task.
This is the most useful fact in agent design. When an agent gives a bad answer, ask one question first: what did the model actually see? Most failures trace back to something that was missing, buried or wrong in that block.
Six parts of a prompt
An agent's prompt usually has six parts, and your program assembles them on every call.
- Instructions. The System promptThe instructions at the start of a prompt that set the model's role, its rules and the format of its answer. It stays the same on every call. holds the role, the rules and the output format. It stays the same on every call.
- The goal. The task the user wants done, in the user's own words.
- The history. What was said and done earlier in this task.
- Tool descriptions. A short list of the tools the model may call, and what each one needs.
- Tool results. What the last actions returned, such as a file's text or a search result.
- Retrieved documents. Passages the program looked up because they relate to the goal.
The instructions and the tool descriptions change rarely. The history, the tool results and the documents grow as the task goes on. That growth is the reason context needs managing, which the next three Lessons cover.
Build the prompt in code
Most model APIs take a list of messages, each with a role. The sketch below assembles one.
def build_prompt(goal, history, documents):
messages = [{"role": "system", "content": "You are a support agent. Answer only from the documents."}]
for passage in documents:
messages.append({"role": "user", "content": f"Document: {passage}"})
messages.extend(history)
messages.append({"role": "user", "content": goal})
return messages
prompt = build_prompt(
goal="Can I return an order after 20 days?",
history=[{"role": "assistant", "content": "Hello! How can I help?"}],
documents=["Refunds are accepted within 30 days of delivery."],
)
for message in prompt:
print(message["role"], "->", message["content"])Print the prompt like this during development. Seeing the real block is the fastest way to find out why an agent behaved as it did.
Order and clarity count
Models do not weigh every part of the prompt equally. Rules at the very start and the latest message at the very end tend to get the most attention. Material in the middle of a long prompt is the easiest to miss.
Three habits help.
- Put the rules first and keep them short. One clear rule works better than five that overlap.
- Label each part. A line such as "Document:" shows the model where a passage begins.
- Keep the goal last so the model finishes reading on the task itself.
What should you check first?
Your agent gave a wrong answer about a refund policy.
The model knows only what is in the prompt. Most failures trace to something missing, buried or wrong in it.
Where do the rules go?
You are assembling a long prompt with rules, documents, history and the user's goal.
Material at the start and end gets the most attention. The middle is the easiest place to lose a rule.
Which part grows as the task runs?
An agent's prompt has a system prompt, tool descriptions, history and tool results.
The system prompt and tool descriptions rarely change. History and tool results grow, which is why context needs managing.
Your reading position is saved
0 of 20 complete
Lesson complete
What goes into a prompt?
Up next