The first story

Let us start with the simplest possible thing: sending a prompt to an LLM and getting text back. Before streaming, before fancy prompts, we need to understand what actually happens when you call an LLM API.

The LLM API call flow

What happens when your FastAPI server talks to an LLM provider.

service.py
python
from utils.llm_provider import get_llm_provider

# The provider pattern: one interface, multiple backends
# Fireworks, OpenRouter, Gemini, OpenAI all work the same way
llm_provider = get_llm_provider()

# This is the core of every LLM application:
# 1. Build a prompt string
# 2. Send it to the provider
# 3. Get text back
prompt = "Tell me a short bedtime story about a brave cat."
response = await llm_provider.generate(prompt)

The provider pattern abstracts away which LLM you are using. Swap providers by changing one environment variable, without touching any code.

If you hardcode the OpenAI SDK, switching to Gemini or a local model means rewriting your service layer. The provider pattern gives you one interface (generate, generate_stream) that works with any backend. You configure which provider to use via an environment variable, and your application code never changes.

Quiz: Quiz

Loading practice…