Environment Setup
Understand how to configure a reliable Python client boundary for hosted LLM APIs by managing environment variables securely, creating isolated virtual environments, and normalizing outputs. This lesson guides you in authenticating requests, handling keys safely, and verifying successful API integration to prepare for building production-ready LLM applications.
Previously, we compared two primary deployment paths: Calling a hosted API and self-hosting open-weight models.
Throughout this lesson, we will focus primarily on the hosted API stack because it represents the fastest path to shipping production LLM features without the operational overhead of GPU cluster management. While self-hosted open weights offer ultimate infrastructure control, mastering the client-side integration patterns, context engineering, and request lifecycles of hosted APIs gives you the core foundation needed for both approaches.
We are starting here by wiring up a thin, reliable client boundary. By the end of this lesson, you will have a working Python script that reads an API key from an environment variable, sends a real request to a provider SDK, and prints two key pieces of output: a short snippet of generated text and the response’s usage metadata.
In the code below, we show a minimal request to Gemini's Messages API and where the generated text and usage information appear in the response.
GEMINI_API_KEY="{{GEMINI_API_KEY}}"Line 1-3: Import the standard library os module, the dotenv loader, and the Google GenAI 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.
Line 8-11: Send a minimal content generation request using the Gemini flash model. ...