Skip to content

Working with LLM APIs at a Glance (B3-2) ​

In the Codyssey B3-2 mission, while building a Python CLI (ai-gitgen) that sends git diff to an LLM API to generate a commit message and a PR draft, I learned how to use an external LLM API safely and cheaply. This page gives a quick tour of the overall flow and concepts first; for details, see the article for each concept.

What Was Built ​

Run python main.py commit or pr, and it collects the Git changes, sends them once to an OpenAI-compatible Chat Completions API (default: Google AI Studio Gemini), checks the returned draft, and prints it in the terminal. It uses only the standard library, and applying the commit or PR is left to the person.

Flow of One Run ​

check API Key (env vars / .env)
 → collect git status / git diff (exit if no changes)
 → (safe-mode) mask sensitive data → limit size
 → build prompt → one API call ({base_url}/chat/completions)
 → validate and post-process output (truncate, normalize headers, warn)
 → print draft + number of calls

Concepts in One Line ​

ConceptOne-line meaningDetails
Env-var key managementRead the key from env vars in priority order, never from codeSafe API Key Management
.env auto-loadingLoad from a file without overwriting shell exportsSafe API Key Management
OpenAI-compatible endpointOne request shape; switch providers by base URL and keyOpenAI-Compatible Endpoints
GET /modelsQuery the models the server supports, liveOpenAI-Compatible Endpoints
Output validationCheck the LLM response against length and section rulesLLM Output Validation and Post-processing
Post-processingFix by truncating and unifying headers instead of re-requestingLLM Output Validation and Post-processing
Sensitive data maskingReplace keys/tokens/emails with [MASKED:type] before sendingSensitive Data Masking
Mask, then cutOrder that keeps partial secrets from surviving at a cut boundarySensitive Data Masking
Token cost(input + output tokens) × model priceManaging LLM API Call Costs
One-call ruleCall once per run, and never make useless callsManaging LLM API Call Costs

What B3-2 Produced ​

ItemContent
Modulesgit_collector (collect), prompts (prompts), ai_client (API call), validators (validation), safe_mode (masking)
Commandscommit, pr, models
Connected toGoogle AI Studio, Codyssey Public API Console
Biggest limitationsafe-mode is off by default and can't catch secrets outside its regex list. No automatic retry on transient errors

Source: Codyssey-B1/B3-2