Environment Setup
Explore how to set up a Python development environment and securely connect to a hosted large language model API. This lesson guides you through managing API keys in environment variables, creating a thin client interface, sending requests, handling responses, and verifying output. You will understand best practices for secrets management and client boundary design to create reliable integrations with hosted AI services.
Previously, we compared two deployment options: calling a hosted API and hosting an open-weight model ourselves.
This lesson focuses on a hosted API. It lets us build a working integration without first managing inference infrastructure. Self-hosting offers more control over that infrastructure, but the client integration skills covered here, including request construction, context management, and response handling, also apply when working with a self-hosted service.
We’ll start by building a small Python client. By the end of the lesson, you’ll have a script that reads an API key from an environment variable, sends a request through a provider SDK, and prints a short excerpt of the generated text and the response’s usage metadata.
The example below uses the Gemini API through the Google Gen AI SDK. It shows where to find the generated text and usage metadata in the response.
GEMINI_API_KEY="{{GEMINI_API_KEY}}"Lines 1–3: Import the standard library os module, the dotenv loader, and the Google Gen AI SDK.
Line 5: Load environment variables automatically from a local
.envfile.Line 7: Construct the client, pulling the key from the environment rather than hardcoding it.
Lines 8–11: Send a minimal content generation request using the Gemini flash model. ...