Inject Mid-Conversation System Messages for Dynamic Agent Control
Update instructions mid-chat without breaking your prompt cache or increasing latency.
Inject system roles directly into the messages array using Claude Opus 4.8 to change agent behavior dynamically while keeping prompt caching active.
The Scenario
You are building a research agent that starts with a general goal but needs to pivot to a specific format or safety constraint after the first few pieces of data are retrieved. instead of restarting the conversation, you need to update its 'rules' mid-stream.
Before & after
You had to update the top-level 'system' field, which invalidated the entire prompt cache and forced the model to re-process thousands of tokens. This could add 10-20 seconds of latency and increase costs per turn.
You append a system message at the specific turn where needed, preserving the original cache. This takes seconds to code and keeps latency low (1-2 seconds) for long sessions.
The Prompt
// Use this structure in your API call to Claude Opus 4.8
{
"model": "claude-3-opus-20240229",
"messages": [
{"role": "user", "content": "[INITIAL_TASK]"},
{"role": "assistant", "content": "[RESPONSE]"},
{"role": "system", "content": "NEW CONSTRAINT: [ADD_INSTRUCTION_HERE]"},
{"role": "user", "content": "[FOLLOW_UP_QUESTION]"}
]
}Mid-conversation system messages allow you to inject new constraints or context into the message history itself. This is especially powerful for agentic workflows where instructions must change based on intermediate results without losing the efficiency of Prompt Caching.
Source
Claude Platform release notes - Claude Platform Docs"Add or update system instructions partway through a conversation without invalidating the cached prefix that came before them."
