Back to Bytes

Implement secure API key management for LLM providers — lab audio overview

2026-04-21

Build a key manager that loads LLM provider API keys from environment variables with a secret-file fallback, fails loudly when a required key is missing, redacts keys for safe logging, and uses the loaded key to make an authenticated LLM call.

GenAI Agent Engineering › Chapter 1 · The Dev Environment › Secure API key management

5:57
Build a key manager that loads LLM provider API keys from environment variables with a secret-file fallback, fails loudly when a required key is missing, redacts keys for safe logging, and uses the loaded key to make an authenticated LLM call.
Share

More from this chapter

Transcript
Host: Welcome back. You're in GenAI Agent Engineering — a course about building AI systems that actually take actions in the real world, not just chat. This chapter is called The Dev Environment, and the goal is to get you set up with a professional Python workspace. Today's exercise sits right at the intersection of two disciplines every AI engineer needs: clean local tooling, and using a large language model to make judgment calls inside your own code. Why does this combination matter in a real team? Expert: Because the first bug most AI teams ship to production isn't a model problem — it's a secrets problem. Picture a mid-sized company, maybe fifty engineers, building an internal AI assistant. Someone pastes a quick prototype into a shared repository. Inside that prototype is a live key — a long string of characters that grants access to a paid service like an AI provider or a cloud database. That key gets pushed to the company's code hosting platform. Within hours, automated bots that scan public code are hitting it, racking up charges, or worse, exfiltrating data. Now multiply that by every engineer, every notebook, every Slack paste. Security teams can't manually review every line. What they need is an automated scanner that reads through text, spots anything that looks like a credential, and — here's the modern twist — uses an AI model to judge how serious each finding actually is. Not every match is a real secret. Some are fake examples in documentation. Some are expired. You need intelligent triage, not just pattern matching. That's exactly the tool you're about to build, and it's the kind of utility a data team can actually deploy internally in an afternoon. Host: So this is your starting point — the chapter overview walked through the big picture of setting up a professional Python environment, and now we're getting specific with a real, useful tool. What exactly are we building here, and what's the core idea behind it? Expert: You're building a small program that does two things in sequence. First, it reads a chunk of text — could be a file, a code snippet, a log — and looks for anything that matches the shape of a known credential. Things like long random strings that start with a known prefix, say the prefix used by OpenAI keys or Google keys or cloud access tokens. That first step is pure pattern matching — fast, local, no AI involved. The second step is where it gets interesting. For each suspicious string the scanner finds, it sends that finding to Gemini — Google's family of large language models — and asks it to classify the risk. Is this a real, active-looking credential? Is it a placeholder like the word "your-api-key-here"? Is it an example from documentation that's obviously fake? Gemini returns a risk level — low, medium, or high — along with a short explanation. Now here's the conceptual aha. Think of this as a two-stage filter. The pattern matcher is cheap and catches everything that looks suspicious — including lots of false alarms. The language model is expensive and slow, but it's smart, so you only call it on the shortlist. This pattern — cheap filter first, smart model second — is one of the most important design patterns in production AI engineering. You'll use it again and again. Secrets scanning is just today's example. Host: Okay, so before someone sits down and starts writing code — what's the one part of this exercise that tends to trip people up? Expert: The part people underestimate is getting the environment itself wired correctly. This lab is in the dev environment chapter for a reason. You'll need to install Python packages, load a secret key from a configuration file so you can talk to Gemini, and make sure that key never ends up committed to your code. The most common mistake is hardcoding the key directly into the program while you're experimenting, then forgetting to remove it. The second common mistake is the opposite — forgetting to set the key up at all, then getting cryptic errors when the program tries to reach Gemini. The fix is to treat the configuration file as the only place your key lives, read it once at startup, and if it's missing, fail loudly with a clear message telling you what to do. The other small trap: when you ask Gemini to classify risk, you want the answer in a predictable shape so your program can act on it automatically. If you just ask in free-form English, you'll get a paragraph back sometimes, a single word other times, and your code will break. So be specific in your instructions to the model — tell it exactly the format you want the answer in, and handle the case where it ignores you anyway. Host: Great. Let's close out — what will someone actually be able to do after finishing this, and what does it unlock for the team? Expert: After this exercise, you'll be able to take any body of text and produce a structured report of every credential-shaped string in it, each one tagged with an AI-generated risk level and explanation. You'll also have a working, tested Python project set up the way professionals set them up — proper dependency management, secret handling, and automated tests you can trust. That's a real building block. Your team can drop this into a pre-commit check, a code review bot, or a nightly scan over internal documents. More broadly, you'll have internalized that two-stage pattern — cheap filter, smart model — which is the backbone of cost-effective AI systems at scale. This is also the only lab in this chapter, so with it finished, you'll have the complete dev environment capability — a working setup you can bring back to your team's architecture discussions as the template for every AI project that follows. Thanks for listening, and good luck with the build.

Want to go deeper? Explore disciplines with hands-on labs, quizzes, and chapter podcasts.