01 Basic Prompt Structure
Chapter 1: Basic Prompt Structure
Setup
Run the following setup cell to load your API key and establish the get_completion helper function.
Lesson
Anthropic offers two APIs, the legacy Text Completions API and the current Messages API. For this tutorial, we will be exclusively using the Messages API.
At minimum, a call to Claude using the Messages API requires the following parameters:
-
model: the API model name of the model that you intend to call -
max_tokens: the maximum number of tokens to generate before stopping. Note that Claude may stop before reaching this maximum. This parameter only specifies the absolute maximum number of tokens to generate. Furthermore, this is a hard stop, meaning that it may cause Claude to stop generating mid-word or mid-sentence. -
messages: an array of input messages. Our models are trained to operate on alternatinguserandassistantconversational turns. When creating a newMessage, you specify the prior conversational turns with the messages parameter, and the model then generates the nextMessagein the conversation.- Each input message must be an object with a
roleandcontent. You can specify a singleuser-role message, or you can include multipleuserandassistantmessages (they must alternate, if so). The first message must always use the userrole.
- Each input message must be an object with a
There are also optional parameters, such as:
-
system: the system prompt - more on this below. -
temperature: the degree of variability in Claude's response. For these lessons and exercises, we have settemperatureto 0.
For a complete list of all API parameters, visit our API documentation.
Examples
Let's take a look at how Claude responds to some correctly-formatted prompts. For each of the following cells, run the cell (shift+enter), and Claude's response will appear below the block.
Now let's take a look at some prompts that do not include the correct Messages API formatting. For these malformatted prompts, the Messages API returns an error.
First, we have an example of a Messages API call that lacks role and content fields in the messages array.
⚠️ Warning: Due to the incorrect formatting of the messages parameter in the prompt, the following cell will return an error. This is expected behavior.
Here's a prompt that fails to alternate between the user and assistant roles.
⚠️ Warning: Due to the lack of alternation between
userandassistantroles, Claude will return an error message. This is expected behavior.
user and assistant messages MUST alternate, and messages MUST start with a user turn. You can have multiple user & assistant pairs in a prompt (as if simulating a multi-turn conversation). You can also put words into a terminal assistant message for Claude to continue from where you left off (more on that in later chapters).
System Prompts
You can also use system prompts. A system prompt is a way to provide context, instructions, and guidelines to Claude before presenting it with a question or task in the "User" turn.
Structurally, system prompts exist separately from the list of user & assistant messages, and thus belong in a separate system parameter (take a look at the structure of the get_completion helper function in the Setup section of the notebook).
Within this tutorial, wherever we might utilize a system prompt, we have provided you a system field in your completions function. Should you not want to use a system prompt, simply set the SYSTEM_PROMPT variable to an empty string.
System Prompt Example
Why use a system prompt? A well-written system prompt can improve Claude's performance in a variety of ways, such as increasing Claude's ability to follow rules and instructions. For more information, visit our documentation on how to use system prompts with Claude.
Now we'll dive into some exercises. If you would like to experiment with the lesson prompts without changing any content above, scroll all the way to the bottom of the lesson notebook to visit the Example Playground.
Exercise 1.1 - Counting to Three
Using proper user / assistant formatting, edit the PROMPT below to get Claude to count to three. The output will also indicate whether your solution is correct.
❓ If you want a hint, run the cell below!
Exercise 1.2 - System Prompt
Modify the SYSTEM_PROMPT to make Claude respond like it's a 3 year old child.
❓ If you want a hint, run the cell below!
Congrats!
If you've solved all exercises up until this point, you're ready to move to the next chapter. Happy prompting!
Example Playground
This is an area for you to experiment freely with the prompt examples shown in this lesson and tweak prompts to see how it may affect Claude's responses.