Message History Formats and Structures
You'll learn: How to structure conversation history using standard role/content pairs that LLM APIs expect.
The Standard Format
When you send messages to an LLM API (like OpenAI's GPT or Anthropic's Claude), they don't just accept raw text. Instead, they expect a structured list of messages where each message has a role and content.
The three standard roles are:
system: Instructions that set behavior (your system prompt)user: Messages from the humanassistant: Responses from the AI
Think of it like a script for a play—each line needs to specify who's speaking and what they said.
Structure Example
A typical conversation history looks like this:
messages = [
{"role": "system", "content": "You are a helpful coding assistant."},
{"role": "user", "content": "How do I reverse a string in Python?"},
{"role": "assistant", "content": "You can use slicing: text[::-1]"},
{"role": "user", "content": "Can you show a full example?"}
]
This array maintains the entire conversation context, allowing the LLM to understand what was previously discussed.
Why This Matters
LLMs are stateless—they don't remember past conversations. You must send the full message history with every API call. The role/content structure ensures:
- Clear speaker identification
- Proper context building across turns
- Consistent formatting across providers
Most frameworks handle this automatically, but understanding the underlying format helps you debug state issues and build custom conversation management.
Key Takeaway: Message history is an ordered list of {role, content} pairs—system instructions first, then alternating user/assistant messages that provide full conversational context to stateless LLMs.