Session 7 Slides: API Integration & Cloud Models

--:-- --
↓ Scroll for more

Session 7

API Integration & Cloud Models

AI Product Engineering

Block 2: AI-Assisted Engineering & Integration

Today’s Agenda

  • How an AI API Call Works
  • Understanding Temperature
  • 🔐 API Key Security
  • Common API Errors
  • Interactive: Keys & Temperature (3 min)
  • Interactive: Error Drill (3 min)
  • Lab: Replace the Mock (Core)

How an AI API Call Works

Your App → API Request (key + model + prompt + params)
Cloud Service → Process (run inference)
API Response → (text + usage + metadata)
Your App → Display response to user

Understanding Temperature

ValueBehaviourUse Case
0.0DeterministicData extraction, classification
0.3–0.7BalancedGeneral Q&A
1.0+CreativeBrainstorming

Start with 0.2–0.5 for reliable production outputs.

🔐 API Key Security

❌ Never

  • Hardcode in source code
  • Commit to git
  • Expose in client-side JS

✅ Always

  • Store in .env file
  • Load with process.env.GEMINI_API_KEY
  • Add .env to .gitignore

Interactive: Keys & Temperature (1/3)

True or false — call on a row.

Interactive: Keys & Temperature (2/3)

True or false — call on a row.

Interactive: Keys & Temperature (3/3)

True or false — call on a row.

Common API Errors

ErrorCauseFix
401Invalid API keyCheck environment variable
429Rate limitExponential backoff
500Server errorRetry with delay, log error
TimeoutRequest too largeReduce input size

Never let an API error crash your app silently.

Interactive: Error Drill (1/3)

Pick the best diagnosis.

Interactive: Error Drill (2/3)

Pick the best diagnosis.

Interactive: Error Drill (3/3)

Pick the best diagnosis.

Lab: Replace the Mock (1/2)

  1. 7.1: .env from example → GEMINI_API_KEY → restart server
  2. 7.2: Replace mock with generateContentserver only (browser talks to /query)

Lab: Replace the Mock (2/2)

  1. 7.3: Pass Session 2 system prompt as systemInstruction
  2. 7.4: Five queries — note quality, latency, errors in prompts.md

Never put the API key in client-side JavaScript.

Session 7 Summary

  • AI API: request → process → response
  • Temperature: use 0.2–0.5 for production
  • API key security: environment variables only
  • Error handling is not optional
  • Your app now has a real AI backend

Next Session: Grounding AI — Embeddings Basics