Sub topic 6
Pinning the system prompt and important messages
This chapter assumes you can call a chat-style LLM API, know the system, user and assistant roles, and have read the previous chapter on sliding-window truncation. You should also be comfortable with the estimate_tokens helper from that chapter.
Why pinning matters
A sliding window keeps the newest messages and drops the oldest. The oldest message in most conversations is the system prompt, and it holds the rules the model must follow: its role, its output format and its safety limits. If the window drops it, the model silently stops following those rules.
Pinning means marking some content as exempt from truncation. Pinned content is always sent, and its token cost is subtracted from the budget before any other message is considered.
What to pin
Pin only content that must be present on every request.
- The system prompt. This is the default pin.
- Hard constraints the user stated, such as "always answer in French" or "never edit files under
/prod". - Key facts the conversation depends on, such as the user's goal or a chosen configuration.
Pinning has a cost. Every pinned token is unavailable for conversation history. If you pin too much, the window shrinks until almost no recent context fits.
How the truncation code pins
The truncate_history function from the previous chapter already pins the system prompt. It does this in three steps.
- It splits the messages into
systemandrest. - It subtracts the cost of the system messages from the budget.
- It fills the remaining budget with the newest messages from
rest.
The code below extends this to pin any message that carries a pinned flag. The flag is a field you add yourself. It is not part of the chat API.
def estimate_tokens(message):
# Rough heuristic: about 4 characters per token, plus a small per-message overhead.
return len(message["content"]) // 4 + 4
def truncate_with_pins(messages, max_input_tokens):
pinned = [m for m in messages if m["role"] == "system" or m.get("pinned")]
rest = [m for m in messages if not (m["role"] == "system" or m.get("pinned"))]
budget = max_input_tokens - sum(estimate_tokens(m) for m in pinned)
if budget < 0:
raise ValueError("Pinned content alone exceeds the input budget")
kept = []
for message in reversed(rest):
cost = estimate_tokens(message)
if cost > budget:
break
kept.append(message)
budget -= cost
kept.reverse()
while kept and kept[0]["role"] != "user":
kept.pop(0)
return pinned + kept
history = [
{"role": "system", "content": "You are a careful code reviewer."},
{"role": "user", "content": "Always answer in French.", "pinned": True},
{"role": "assistant", "content": "D'accord."},
{"role": "user", "content": "Review this function. " * 20},
{"role": "assistant", "content": "Here is my review. " * 20},
{"role": "user", "content": "Now check the tests."},
]
result = truncate_with_pins(history, 120)
for m in result:
print(m["role"], "|", m["content"][:40])Two details matter here. The function raises an error when the pinned content alone exceeds the budget, because continuing would send a request that cannot fit. The pinned messages also come first in the output, so a pinned user message moves ahead of the chronological order. For a standing instruction this is usually fine, but a pinned message that refers to an earlier turn can lose its meaning.
Where pinned content sits
The model reads the request from top to bottom, so the layout affects how well it follows the pinned content.
The order is stable content first and changing content last. Some providers cache a repeated prefix of the request. If the pinned part is identical on every call, the cache can reuse it, and a changing prefix defeats that.
Choosing what to pin
| Candidate | Pin it? | Reason |
|---|---|---|
| System prompt | Yes | Defines behavior on every turn |
| Stated hard constraint | Yes | Forgetting it causes visible errors |
| User's overall goal | Usually | Short, and the rest of the chat depends on it |
| A long pasted document | No | Large; summarize it or keep a reference instead |
| Small talk | No | Safe to lose |
Exercise
A request has a 1,000-token input budget. The system prompt costs 200 tokens and two pinned user constraints cost 150 tokens together. How many tokens remain for recent conversation, and what should the code do if the pinned content grows to 1,100 tokens?
Show answer
650 tokens remain (1,000 - 200 - 150). If the pinned content reaches 1,100 tokens, the budget goes negative and no history fits. The code should raise an error, as truncate_with_pins does. A better fix is to shorten the pinned content or move part of it into a summary.