GenAI & Agentic AI Workshop · Day 1 · Session 3
From chatting with AI to building with it
Press → to move · S for speaker notes · F for full screen
Food-delivery support bots, review summaries, your college's future helpdesk...
They don't open ChatGPT.
Their code calls an API.
Place an order (request)
Runs on Google's / Groq's servers
The waiter (API) takes the order and brings back the food (response). You don't need to know how the kitchen works.
GEMINI_API_KEY → Notebook access ONuserdata.get("GEMINI_API_KEY")🚫 Never paste the key in code and push to GitHub. Bots find leaked keys in minutes.
from google import genai
from google.colab import userdata
client = genai.Client(api_key=userdata.get("GEMINI_API_KEY"))
MODEL = "gemini-2.5-flash"
resp = client.models.generate_content(
model=MODEL,
contents="Explain what a compiler does in 2 lines, for a first-year student.",
)
print(resp.text)
Client + model + text. That's it.
resp = client.models.generate_content(
model=MODEL,
contents="Suggest 3 names for our college coding club.",
config=types.GenerateContentConfig(
system_instruction="You are a creative assistant. Reply as a numbered list.",
temperature=1.0, # Session 1
max_output_tokens=200, # limit length (and cost)
),
)
Response also has usage_metadata (tokens) and finish_reason (STOP or MAX_TOKENS).
| Role | Who | Example |
|---|---|---|
| system | Developer (hidden) | "You are CampusBuddy..." |
| user | The person | "When is the DBMS exam?" |
| model / assistant | The AI's earlier replies | "12 Dec at 10 AM." |
Gemini says model; OpenAI-style APIs (Groq) say assistant.
history = []
def chat(user_text):
history.append({"role": "user", "parts": [{"text": user_text}]})
resp = client.models.generate_content(
model=MODEL, contents=history, # send EVERYTHING
config=types.GenerateContentConfig(system_instruction=SYSTEM))
history.append({"role": "model", "parts": [{"text": resp.text}]})
return resp.text
chat("Hi, I'm Priya from CSE.")
chat("What's my name and branch?") # → Priya, CSE ✅
Change contents=history → contents=user_text and it forgets ❌
for chunk in client.models.generate_content_stream(model=MODEL, contents=prompt):
print(chunk.text or "", end="", flush=True)
Illustrative prices: $0.30 / million input tokens, $2.50 / million output tokens
| 2,000 students × 10 questions a day | Tokens | Cost | |
|---|---|---|---|
| Input | × 500 tokens (prompt + history) | 10 M | $3.00 |
| Output | × 200 tokens | 4 M | $10.00 |
| Total per day | $13 ≈ ₹1,100 | ||
Save: smaller model · short system prompt · trim history · max_output_tokens · cache common answers
| Error | Meaning | Fix |
|---|---|---|
| 400 | Bad request | Fix your code |
| 401 / 403 | Bad or missing key | Check the secret |
| 429 | Rate limit hit | Wait, retry with backoff |
| 500 / 503 | Provider problem | Wait, retry |
60 students on free keys = some 429s. Normal! Wait a minute and retry.
model + messages + settings
→ text + token usage
client.models.generate_content(...)
client.chat.completions.create(...)
Keep a Groq key as a backup if one provider is slow or rate-limited.
cost()chat() with history. Prove it remembers youStretch: keep only the last 6 messages · retry on 429 · switch to Groq
Up next · Session 4
Make CampusBuddy answer from your college's own documents, with sources
Questions? 🙋