Sub topic 12
Using the Voice Guide as a System Prompt in Code
This chapter assumes you have a written voice guide, as built in the earlier chapters, and basic Python experience. You should also have an API key for an AI provider. The examples use Anthropic's Python SDK.
Why use the API
In a chat app, your custom instructions apply to your own conversations. When your product generates text in code, such as emails, support replies, or product descriptions, you have to send the instructions yourself with each request. In the API, the field for this is called the system prompt. It holds standing instructions. The user message holds the task for this one request.
Because the voice guide lives in a file, every request that loads it gets the same voice. It also makes the guide easy to review, version, and share with your team.
Load the guide and send it
Save your voice guide as voice_guide.md. This code reads the file and passes it as the system prompt.
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment
with open("voice_guide.md", encoding="utf-8") as f:
voice_guide = f.read()
response = client.messages.create(
model="YOUR_MODEL_ID", # replace with a model ID available to your account
max_tokens=500,
system=voice_guide,
messages=[
{"role": "user", "content": "Draft a two-sentence welcome email for new users."}
],
)
print(response.content[0].text)Replace YOUR_MODEL_ID with a model your account can use. The system argument carries the voice. The messages list carries only the task.
What changes compared with a chat app
Several things differ from the chat settings you used earlier:
- No memory between calls. Each request is independent. Send the system prompt every time.
- Your code owns the layers. In chat, the app combines account-level and project-level instructions for you. In code, you decide what goes into
system. If you want a company-wide voice plus a task-specific rule, concatenate them yourself. - Output limits are yours to set. The
max_tokensvalue caps the reply length. A cap that is too low cuts a reply off mid-sentence, so treat it as a safety limit and keep length rules in the guide too. - Cost and speed. The guide is sent with every request, so a long guide adds to every call. Keep it as short as it can be while staying clear.
Combining a base guide with a task rule
A common pattern is one shared voice guide plus a short rule per use case.
def build_system_prompt(voice_guide: str, task_rule: str = "") -> str:
"""Return the voice guide, followed by an optional task-specific rule."""
if not task_rule:
return voice_guide
return f"{voice_guide}\n\nFor this task only:\n{task_rule}"
# Example: build a prompt for short subject lines
prompt = build_system_prompt(
"Voice: friendly and plain-spoken.",
"Write subject lines under 50 characters.",
)
print(prompt)Put the task rule after the shared guide, and make sure it does not contradict it. Conflicting instructions are a common cause of inconsistent output.
Test through the same path
Run your fixed test prompts through the code, not only in a chat window. Loop over the prompts, send each one in a fresh request, and save the replies next to the version of the guide that produced them. Then check each reply against your checklist, as you did in the testing chapter.
Common errors
| Problem | Likely cause | Fix |
|---|---|---|
| Replies ignore the voice | Guide was not passed in `system` | Check that `system=voice_guide` is in the call |
| Reply is cut off | `max_tokens` is too low | Raise it, and keep length rules in the guide |
| Authentication error | API key is missing from the environment | Set `ANTHROPIC_API_KEY` and restart your shell |
| Tone differs between tasks | Task rule conflicts with the guide | Reword the rule so it adds to the guide |
Exercise
Your code sends the voice guide in the system field, but a reply stops in the middle of a sentence. Name the most likely cause and one fix.
Show answer
The maxtokens limit is too low, so the model stops when it reaches it. Raise maxtokens, and state the length you want in the voice guide so replies finish naturally within the limit.
Exercise
You want one company-wide voice but a stricter rule for subject lines. How do you build the system prompt, and what should you check?
Show answer
Concatenate the shared voice guide with a short task-specific rule placed after it, as buildsystemprompt does. Check that the task rule does not contradict the shared guide, because conflicting instructions cause inconsistent output.