Write and test a concise system prompt for a code-focused assistant.

Download the runnable notebook

This complete lesson was converted from the matching notebook stored locally in the TechClub repository. Outputs are cleared; never add API keys or private data.

Goal, prerequisites, and setup

Apply the system-prompt lesson by changing only the instruction that controls response style. Complete 002_system_prompt first; know Python functions and environment variables. Install anthropic and python-dotenv, set ANTHROPIC_API_KEY and ANTHROPIC_MODEL, and keep the key outside the notebook.

Test the same coding request with the baseline and revised prompt. Score concision, correctness, readability, and whether the answer follows the requested format.

# Load environment variables
from dotenv import load_dotenv

load_dotenv()
# Create an API client
import os
from anthropic import Anthropic

client = Anthropic()

model = os.getenv("ANTHROPIC_MODEL")
# Helpers
def add_message(messages, content, role):
    message = {"role": role, "content": content}
    messages.append(message)

def chat(messages, system=None):
    params = {
        "model": model,
        "max_tokens": 200,
        "messages": messages,
    }

    if system:
        params["system"] = system

    message = client.messages.create(**params)

    return message.content[0].text
messages = []

"""
QUESTION:
-> Make claude to respond as concise as possible.
"""

system_prompt = """
# You are a expert Python programmer.
# Answer the user's questions with clear, short and concise code examples.
You are a Python engineer who writes very concise code.
"""

add_message(messages,
            "Write a Python function that checks a string for duplicate characters.",
            role="user")
answer = chat(messages, system=system_prompt)

print(answer)

Evidence and references