Injecting Persistent Behaviors
Rules that apply across all turns: always respond in JSON, never discuss X.
What Are Persistent Behaviors?
Persistent behaviors are rules that apply to every response the model generates, regardless of what the user asks. They are defined in the system prompt and never change during a conversation session.
Common persistent behaviors:
- Always respond in JSON
- Never discuss competitors
- Always ask for clarification before writing code
- Always cite sources
- Always use a specific language or tone
Always Respond in JSON
Forcing the model to always return JSON makes output programmatically predictable. The system prompt must be explicit about this requirement:
import anthropic, json
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
SYSTEM_JSON = '''
You must ALWAYS respond with a valid JSON object. No prose, no markdown, no code fences.
Every response must have at minimum: {"response": "string", "confidence": "high|medium|low"}
If you cannot answer, return: {"response": null, "confidence": "low", "reason": "string"}
'''
def ask(question):
r = client.messages.create(
model='claude-opus-4-5', max_tokens=300,
system=SYSTEM_JSON,
messages=[{'role': 'user', 'content': question}]
)
return json.loads(r.content[0].text)
result = ask('What is the capital of France?')
print(result['response']) # Paris
print(result['confidence']) # highAll lessons in this course
- System vs User Role Distinction
- Injecting Persistent Behaviors
- Persona and Role Definition
- Testing System Prompt Effectiveness