Prompting Strategies and Tips › Giving Your Agent a Role

Michael BrenndoerferJune 13, 20252 min read
AI Agent HandbookAll chapters
Part 3 of 15Prompting - Communicating with Your AI
Chapter 8Prompting Strategies and Tips › Giving Your Agent a Role
Reading section 2 of 10 · Giving Your Agent a Role · 2 minSection 2 of 10 · 2 min

Book contents

Section 2 of 10 · 2 min read

Prompting Strategies and Tips › Giving Your Agent a RoleOne section · Sections in this chapter2 / 10 ↓

One of the simplest yet most powerful prompting strategies is telling the AI who it should be. When you assign a role, you're giving the model context about how to approach the task. This shapes both what it says and how it says it.

Here's a basic example. If you ask:

Explain photosynthesis.

You'll get a reasonable explanation. But watch what happens when you add a role:

You are a biology teacher explaining concepts to high school students. 
Explain photosynthesis in a way that's easy to understand.

The second version will likely use simpler language, include helpful analogies, and avoid jargon. The role gives the model a framework for how to structure its response.

Why Roles Work

Language models are trained on vast amounts of text, including conversations, articles, and documents where people adopt different roles. When you specify a role, you're essentially activating patterns the model has learned about how that type of person communicates.

A few common roles that work well:

  • Teacher or tutor: Good for explanations that need to be clear and educational
  • Expert consultant: Useful when you want detailed, technical information
  • Friendly assistant: Helps when you want a conversational, approachable tone
  • Critical reviewer: Effective when you need the AI to analyze or critique something

The key is matching the role to your goal. If you want creative brainstorming, you might say "You are a creative director." If you need careful analysis, try "You are a data analyst reviewing this information."

Example (GPT-5)

Let's see how roles change the output. Here's a simple Python example using OpenAI's API:

In[3]:
Code
import os
from openai import OpenAI


## Initialize client with your API key
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

## Without a role
## GPT-5 excels at basic text generation tasks like explanations
response1 = client.chat.completions.create(
    model="gpt-5",
    messages=[{"role": "user", "content": "Explain machine learning."}],
)

## With a teacher role
response2 = client.chat.completions.create(
    model="gpt-5",
    messages=[
        {
            "role": "system",
            "content": "You are a patient teacher explaining concepts to beginners.",
        },
        {"role": "user", "content": "Explain machine learning."},
    ],
)

print("Without role:", response1.choices[0].message.content)
print("\nWith teacher role:", response2.choices[0].message.content)
Out[3]:
Console
Without role: Machine learning (ML) is the study and practice of building systems that learn patterns from data to make predictions or decisions without being explicitly programmed with fixed rules. In Tom Mitchell’s classic phrasing: a program learns from experience E, on task T, with performance P, if its performance at T improves with experience E.

Why use it
- When rules are too complex to hand-code.
- To generalize from past examples to new, unseen cases.

Main types
- Supervised learning: learn from labeled examples (e.g., predict house prices, classify emails as spam/not spam).
- Unsupervised learning: find structure in unlabeled data (e.g., clustering customers, dimensionality reduction).
- Semi/self-supervised: leverage large unlabeled data with limited or synthetic labels.
- Reinforcement learning: learn actions via trial and error to maximize reward (e.g., game playing, robotics).
- Generative modeling: learn data distributions to generate or model new samples (e.g., text, images, audio).

Core concepts
- Data and features: inputs (and labels for supervised tasks). Data quality dominates outcomes.
- Model: a function with parameters (e.g., linear model, neural network) mapping inputs to outputs.
- Loss/objective: measures error; training minimizes this (e.g., MSE, cross-entropy).
- Optimization: algorithms like gradient descent adjust parameters to reduce loss.
- Generalization: perform well on unseen data; avoid overfitting.
- Regularization and capacity: techniques (L2, dropout, early stopping) to control model complexity.
- Hyperparameters: settings not learned directly (learning rate, depth) tuned via validation.

Typical workflow
1. Define the problem and success metric.
2. Collect and explore data; handle missing values, leakage, and biases.
3. Split data into train/validation/test (or use cross-validation).
4. Engineer features or use representation learning (deep nets).
5. Train baseline models; iterate with tuning and regularization.
6. Evaluate with appropriate metrics (e.g., MAE/MSE/R²; accuracy/precision/recall/F1/ROC-AUC).
7. Deploy, monitor for drift, fairness, and reliability; retrain as needed.

Common algorithms
- Regression/classification: linear and logistic regression, decision trees, random forests, gradient boosting (XGBoost/LightGBM), SVM, k-NN, Naive Bayes.
- Unsupervised: k-means, hierarchical clustering, Gaussian mixtures, PCA/t-SNE/UMAP.
- Deep learning: neural networks (MLP), CNNs for images, RNN/LSTM/Transformers for sequences and language; diffusion and autoregressive models for generation.
- Reinforcement learning: Q-learning, policy gradients, actor-critic.

Applications
- Recommendation, search, fraud detection, forecasting, medical diagnosis, language translation, speech/image recognition, autonomous systems, personalization.

Pitfalls and considerations
- Data leakage and distribution shift.
- Class imbalance and calibration.
- Bias, fairness, and privacy.
- Interpretability vs. accuracy trade-offs.
- Correlation vs. causation; spurious patterns.
- Computational cost and energy.

Quick example
- Spam filter (supervised classification): collect labeled emails, extract features (or use a Transformer), train to minimize classification loss, tune on validation, evaluate with precision/recall, deploy and monitor for new spam tactics.

If you tell me your context (e.g., the kind of data and task), I can suggest a concrete approach and starter models.

With teacher role: Machine learning (ML) is a way of getting computers to learn patterns from data so they can make predictions or decisions without being told exact rules for every situation.

A simple analogy
- Imagine teaching a child to recognize cats. You don’t write a rule like “if it has whiskers and pointy ears, it’s a cat.” Instead, you show lots of pictures labeled “cat” or “not cat.” Over time, the child notices patterns and gets better at telling cats apart. ML works the same way, but with math.

Core ideas in plain terms
- Data: The examples the computer sees. Each example has inputs (features). Sometimes it also has the correct answer (label).
- Model: A flexible set of rules the computer can adjust to fit the data (like knobs it can turn).
- Training: The process of adjusting those knobs so the model’s predictions match the known answers.
- Generalization: Doing well on new, unseen data—not just memorizing the training examples.
- Prediction (inference): Using the trained model to make a guess for new input.

Main types of machine learning
- Supervised learning: Learn from input-output pairs.
  - Classification: Predict categories (spam vs not spam, disease vs no disease).
  - Regression: Predict numbers (house price, temperature).
- Unsupervised learning: Find structure without labels.
  - Clustering: Group similar items (customer segments).
  - Dimensionality reduction: Compress data to its essential factors (visualize high‑dimensional data).
- Reinforcement learning: Learn by trial and error with rewards (game-playing AIs, robot control).

A quick end-to-end example
- Goal: Predict house price.
- Data: For each house, features like size, bedrooms, location; label is the actual sale price.
- Train: Show many examples; the model adjusts to reduce the gap between predicted and real prices.
- Use: For a new house, the model estimates the price based on learned patterns.

Typical workflow
1) Define the problem and what “good” means (metric like accuracy, F1, mean error).
2) Collect data and clean it (handle missing values, fix errors).
3) Split into train/validation/test sets to check generalization.
4) Choose and train a model; tune settings (hyperparameters).
5) Evaluate on validation/test data; fix issues like overfitting.
6) Deploy the model; monitor performance and update as the world changes.

Common algorithms (with one‑line intuition)
- Linear regression/logistic regression: Fit a straight-line relationship; fast and interpretable.
- Decision trees and random forests: If‑then rules learned from data; handle mixed features well.
- Gradient boosting (e.g., XGBoost, LightGBM): Many small trees combined; often top tabular performers.
- k‑nearest neighbors: Look at the most similar past examples to vote on the answer.
- Support vector machines: Find a boundary that best separates classes.
- Neural networks (deep learning): Stacked layers that learn complex patterns (images, audio, language).

Key challenges and pitfalls
- Overfitting: Model memorizes training data and fails on new data. Fix with more data, simpler models, regularization, or better validation.
- Underfitting: Model too simple to capture real patterns.
- Data leakage: Test information accidentally sneaks into training, giving overly optimistic results.
- Bias and fairness: If training data reflects historical bias, the model can learn and amplify it. Requires careful data design and evaluation.
- Data drift: The world changes; models decay over time and need retraining.
- Data needs and quality: Poor or too little data limits performance more than fancy algorithms.

When to use (and not use) ML
- Use ML when rules are hard to write by hand but examples are available (image recognition, speech, recommendations).
- Prefer simple rules or traditional software when the task is clear-cut, constraints are strict, or explanations are critical and data is scarce.

How ML differs from traditional programming
- Traditional: You give rules + data → outputs.
- ML: You give data + outputs (labels) → the system learns rules to produce outputs for new data.

If you’d like, tell me an area you care about (health, finance, apps, games), and I can walk through a concrete, beginner-friendly example in that context.

Notice how we use the system message to set the role. This tells the model to maintain that persona throughout the conversation. The system message acts like stage directions in a play, setting the scene before the dialogue begins.