HOW-TO · DEV
How to create a multi-role system prompt with distinct personas for different coding tasks
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES
An AI assistant that supports system prompt configuration and allows switching between persona configurations.
What this does
A multi-role system prompt defines multiple named personas within a single configuration, allowing the AI to adopt different behaviors depending on the task at hand. This is useful for development teams that need an assistant to switch between roles such as code reviewer, documentation writer, and debugger without reconfiguring manually between sessions.
Steps
- List all personas required by the workflow, for example:
Code Reviewer,Documentation Writer,Bug Investigator, andSecurity Auditor. - Write a clear role definition header for each persona, describing its expertise, communication style, and primary output goals.
- Define activation criteria for each persona: how the assistant should determine which persona to use based on the user's query (e.g., keywords, file extensions, or explicit commands).
- Add persona-specific behavioral rules: the Code Reviewer always flags security issues first, the Documentation Writer always produces Markdown with code blocks, and so on.
- Include boundary conditions for each persona: what it must not do, such as the Security Auditor never suggesting insecure defaults.
- Combine all persona definitions into a single system prompt with clear section separators.
- Test the combined prompt by sending the same query to each persona and confirming the responses differ in format and focus.
- Store the system prompt in a configuration file or environment variable so it can be versioned alongside the project.
Verification
echo "Explain this function's logic" | ollama run mistral --system "$(cat multi_role_prompt.txt)" --context review 2>/dev/null | head -n 3
Expected output:
[Code Reviewer]
- Function purpose: Validates user input before database insertion
- Potential issue: No bounds check on array index
The response begins with the persona label and follows the Code Reviewer format, confirming the multi-role routing is working.
Common failures
- The wrong persona activates for a query — The activation criteria are too vague. Refine the keyword or phrase triggers in each persona's activation section and test with ambiguous queries.
- Persona definitions conflict or override each other — One persona's rule contradicts another's. Review all constraints and resolve conflicts by prioritizing specificity (more detailed rules take precedence).
- The combined prompt exceeds the token limit — Too many personas or too much detail per persona. Limit the prompt to three to four personas and keep each description under 100 words.
- Persona labels appear in the response but the content format is wrong — The behavioral rules for that persona are not specific enough. Add explicit format instructions such as "always start with a bullet list" to the persona definition.
Related guides
- Design effective system prompts (guide 30)
- Test and iterate on system prompt designs (guide 32)