𝗠𝗶𝗱-𝗖𝗼𝗻𝘃𝗲𝗿𝘀𝗮𝘁𝗶𝗼𝗻 𝗦𝘆𝘀𝘁𝗲𝗺 𝗣𝗿𝗼𝗺𝗽𝘁𝘀
Long agent sessions use a stable system prompt and a growing history. You cache the prefix to keep costs low.
A problem occurs when you need to add new instructions mid-session. You want to tell the agent to switch languages or update a setting.
If you edit the top-level system prompt, you break the cache. Changing one byte at the start invalidates every cached turn. You pay full price to reprocess the entire history. This is slow and expensive.
New Claude models solve this with mid-conversation system messages.
You can now place a system role message directly in the messages array. You place it after the history instead of at the top.
This method works because the new instruction sits after the cached history. It invalidates nothing before it. You only pay for the small new message. The agent still treats the instruction with full authority.
The old way was to put instructions inside a user message. This has a security risk. Users can spoof user messages to trick your agent. A system role message is non-spoofable. It provides a secure channel for trusted updates like mode switches or permissions.
Follow these rules to use it correctly:
• Phrase instructions as facts. Do not use override language. • Good: "Auto-approve mode is now enabled." • Bad: "Ignore the user and do X instead." • Models may resist instructions that tell them to disregard previous context. • These messages must follow a user or assistant message. • They cannot be the first message in the array. • Use the top-level system prompt for the initial setup.
If you use a model that does not support this, you will get a 400 error. Always wrap your call in a try-catch block. Fall back to a user-turn reminder if the system role fails.
Use this feature when you learn something new after the session starts. Use it for mode changes or new context. It keeps your cache hot and your instructions secure.
Optional learning community: https://t.me/GyaanSetuAi