Skip to content

Learn Agentic AI by Building One — A Hands-On Guide - Lesson 2: Prompt Engineering — Shaping AI Behavior

4 minute read
Content level: Intermediate
0

Introduction to how Agentic AI works behind the scene

The same LLM can act completely differently based on HOW you ask. This is "prompt engineering" — crafting instructions to get the output you want.

Two key concepts:

  • System prompt: Sets the AI's personality/role (invisible to the "user")
  • User prompt: The actual question or task

Think of it like hiring someone. The system prompt is the job description. The user prompt is the task you give them.

System: "You are a pirate"  +  User: "Tell me about Python"
→ "Arrr, Python be a fine language for codin' on the high seas!"

The Code

We extend our call_llm function with an optional system prompt:

import json
import boto3

bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")


def call_llm(user_prompt: str, system_prompt: str = None) -> str:
    """Now with an optional system prompt to control behavior."""

    body = {
        "anthropic_version": "bedrock-2023-05-31",
        "max_tokens": 512,
        "messages": [{"role": "user", "content": user_prompt}],
    }

    # System prompt is separate — it's the AI's "instructions"
    if system_prompt:
        body["system"] = system_prompt

    response = bedrock.invoke_model(
        modelId="us.anthropic.claude-sonnet-4-20250514-v1:0",
        contentType="application/json",
        accept="application/json",
        body=json.dumps(body),
    )
    result = json.loads(response["body"].read())
    return result["content"][0]["text"]

Same Question, Different System Prompts

Let's ask the same question — "Explain what a variable is in programming" — with different system prompts and see how the behavior changes.

No system prompt (default behavior):

call_llm("Explain what a variable is in programming.")

Output:

A variable is a named storage location in a computer's memory that holds data which can be used and manipulated throughout a program.

Key Characteristics

Name: Variables have identifiers (names) that you use to reference them

age = 25        # 'age' is the variable name
username = "John"   # 'username' is the variable name

Think of Variables Like Containers

Imagine variables as labeled boxes:

  • The label is the variable name
  • The contents are the value stored inside
  • You can change the contents but keep the same label
  • You can look inside anytime by using the label

Teacher for 5-year-olds:

call_llm(
    "Explain what a variable is in programming.",
    "You are a teacher for 5-year-olds. Use simple analogies. Keep it to 2 sentences."
)

Output:

A variable in programming is like a special box where you can store things like numbers or words, and you can put a label on the box so you remember what's inside. Just like how you might have a toy box labeled "toys" or a crayon box labeled "crayons," programmers use variables to keep track of important information in their computer programs.

Sarcastic senior developer:

call_llm(
    "Explain what a variable is in programming.",
    "You are a sarcastic senior developer. Be brief. Max 2 sentences."
)

Output:

A variable is just a labeled box where you store data that you'll probably forget the purpose of within a week. Think of it as giving a nickname to some piece of information so your computer doesn't have to remember "that thing at memory address 0x7fff5fbff6ac."

Constrained to a topic:

This is how real apps work — the system prompt keeps the AI on-topic:

call_llm(
    "What's a good recipe for pasta?",
    "You are a support case analyst. You ONLY discuss support case metrics and trends. "
    "If asked about anything else, politely decline. One sentence max."
)

Output:

I focus specifically on support case metrics and trends, so I can't help with cooking recipes. Please ask me about support case analysis instead.

The system prompt constrained the AI to stay on topic — it refused to answer a pasta question because it was told it's a support analyst.

Key Takeaway

System prompts are how you build AI "characters" or "roles." Every AI chatbot, copilot, and agent starts with a carefully crafted system prompt. The model is the same — the prompt is what makes it behave differently.

Next up: Lesson 3 — Getting structured data back (not just free text) →

Previous: Lesson 1 — What is an LLM? Making Your First AI Call→