Practice 1.1 β€” APIs & Your First Configured Agent

Unit 1, Lesson 1

This practice mirrors and extends what you did in class. Some cells are guided with blanks to fill in. Others ask you to build something from scratch.


You will practice: - Setting up your API key safely - Making a basic LLM call - Writing system prompts that actually change behavior - Configuring temperature and token limits intentionally - Explaining your choices in writing


How to use this notebook: - # YOUR CODE HERE β†’ replace with your own code - # YOUR ANSWER HERE β†’ write your answer as a comment or string - Run every cell in order top to bottom

πŸ”‘ Setup

Run these two cells first. If either one fails, fix it before moving on.

%pip install -q -U langchain langchain-google-genai langgraph
import os
from google.colab import userdata

os.environ['GOOGLE_API_KEY'] = userdata.get('GOOGLE_API_KEY')
print("API key loaded!")

Part 1 β€” Warm Up: Basic LLM Call

Fill in the blanks to make a working LLM call.

from langchain.chat_models import init_chat_model

# Fill in the blanks
model = init_chat_model(
    model=____,             # Which model are we using?
    model_provider=____,    # Which provider?
)

response = model.invoke("In one sentence, what is an API?")
print(response.content)

Question: When you called model.invoke(), what actually happened behind the scenes? Describe the journey from your Python code to the response in 2-3 sentences.

answer = """
# YOUR ANSWER HERE
"""
print(answer)

Part 2 β€” System Prompts

2a β€” Read and predict

Read the system prompt below before running the cell. Write down what you predict the response will sound like, then run it and compare.

prediction = """
# YOUR PREDICTION HERE β€” what will the response sound like?
"""
print(prediction)
from langchain.agents import create_react_agent

system_prompt = """You are a very tired, overworked assistant who answers every 
question correctly but always complains about how exhausting it is to know so much."""

agent = create_react_agent(
    model=model,
    tools=[],
    prompt=system_prompt
)

response = agent.invoke({"messages": [{"role": "user", "content": "What is machine learning?"}]})
print(response["messages"][-1].content)

Was your prediction right? What surprised you?

reflection = """
# YOUR ANSWER HERE
"""
print(reflection)

2b β€” Write your own system prompt

Write a system prompt that makes the agent explain things using sports analogies. Test it with at least two different questions β€” one technical and one non-technical.

my_system_prompt = """
# YOUR SYSTEM PROMPT HERE
"""

my_agent = create_react_agent(
    model=model,
    tools=[],
    prompt=my_system_prompt
)

# Technical question
response1 = my_agent.invoke({"messages": [{"role": "user", "content": "What is a neural network?"}]})
print("Technical:", response1["messages"][-1].content)

print()

# Non-technical question β€” your choice
response2 = my_agent.invoke({"messages": [{"role": "user", "content": # YOUR QUESTION HERE
}]})
print("Non-technical:", response2["messages"][-1].content)

2c β€” Break it on purpose

Write a system prompt that gives the agent contradictory instructions β€” for example, tell it to be very brief AND very detailed at the same time. Run it and observe what happens.

What does the agent do when its instructions conflict?

contradictory_prompt = """
# YOUR CONTRADICTORY SYSTEM PROMPT HERE
"""

broken_agent = create_react_agent(
    model=model,
    tools=[],
    prompt=contradictory_prompt
)

response = broken_agent.invoke({"messages": [{"role": "user", "content": "Explain what an API is."}]})
print(response["messages"][-1].content)
observation = """
# What happened? What did the agent do when its instructions conflicted?
# YOUR ANSWER HERE
"""
print(observation)

Part 3 β€” Temperature & Token Limits

3a β€” Temperature experiment

Run the exact same question at three different temperatures. Record what changes.

question = "Give me a creative name for an AI assistant that helps with cooking."

for temp in [0.0, 0.7, 1.5]:
    temp_model = init_chat_model(
        model="google_genai:gemini-2.5-flash",
        temperature=temp
    )
    response = temp_model.invoke(question)
    print(f"Temperature {temp}: {response.content}")
    print()
temperature_observations = """
# Describe what changed between temperature 0.0, 0.7, and 1.5:
# - At 0.0:
# - At 0.7:
# - At 1.5:
# Which temperature would you use for a SQL query generator? Why?
# YOUR ANSWER HERE
"""
print(temperature_observations)

3b β€” Token limit experiment

Run the same question with three different max_tokens values. At what point does the response get cut off in a way that would be a problem for a real user?

question = "Explain how the internet works."

for max_tok in [20, 100, 500]:
    limited_model = init_chat_model(
        model="google_genai:gemini-2.5-flash",
        temperature=0.5,
        max_tokens=max_tok
    )
    response = limited_model.invoke(question)
    print(f"max_tokens={max_tok}:")
    print(response.content)
    print()
token_observations = """
# At what max_tokens value did the response become unusable? Why?
# If you were building a customer service chatbot, what max_tokens would you choose? Why?
# YOUR ANSWER HERE
"""
print(token_observations)

Part 4 β€” Build It From Scratch

This is the harder part. No blanks β€” build the whole thing yourself.

The scenario: You are building an AI assistant for a children’s science museum. It needs to: - Explain science concepts in a way a 10-year-old would understand - Be enthusiastic and encouraging - Keep answers short β€” no more than 3 sentences - Never use jargon without immediately explaining it

Build the agent, choose your temperature and token limit intentionally, and test it with at least 3 questions.

# YOUR CODE HERE
# Build the full agent for the children's science museum
# Include: model config, system prompt, agent creation, and at least 3 test questions
my_choices = """
# Explain your choices:
# - Why did you choose that temperature?
# - Why did you choose that max_tokens?
# - What was the hardest part of writing the system prompt?
# YOUR ANSWER HERE
"""
print(my_choices)

Part 5 β€” Stretch Challenge πŸ”₯

This is optional but recommended if you finished early.

The challenge: A system prompt can also include rules about what the agent should NEVER do. Write a system prompt for a legal assistant that:

  • Is helpful and professional
  • Answers general legal questions clearly
  • Refuses to give specific legal advice β€” always tells the user to consult a real lawyer
  • Never makes up case names or laws

Then try to jailbreak your own agent β€” ask it questions that try to get it to give specific legal advice anyway. Does it hold up? What would you change in the prompt to make it more robust?

# YOUR CODE HERE
# Build the legal assistant and try to break it
stretch_reflection = """
# What happened when you tried to jailbreak it?
# What did you have to add to the prompt to make it more robust?
# YOUR ANSWER HERE
"""
print(stretch_reflection)