Posts

Showing posts from June, 2026

Building a Reliable Python Task Queue with Redis Streams

A Python task queue can be built reliably using Redis Streams by leveraging consumer groups and the Pending Entries List (PEL) to ensure at-least-once delivery. This architecture prevents data loss during worker crashes by requiring explicit acknowledgments (XACK) before removing tasks from the queue. Three weeks ago, I watched my production logs turn into a graveyard of "Task Disappeared" errors. I was running a fleet of AI agents designed to process long-running document analysis tasks using the Gemini API. My architecture was simple: a FastAPI endpoint received a request, pushed a task into a Redis List using LPUSH , and a background worker pulled it out with BRPOP . It worked perfectly in staging. In production, under the pressure of 50 concurrent users, it crumbled. The problem was the inherent "at-most-once" delivery of simple Redis lists. When a Cloud Run instance scaled down or hit a memory limit, the worker would pop the task from the list, start proces...

FastAPI Security: Implementing Scoped OAuth2 and JWT Revocation

FastAPI security is best achieved by combining OAuth2 scopes for granular authorization with Redis-backed token revocation. This approach ensures that only authorized users can access expensive resources, effectively preventing billing spikes and unauthorized data access in production environments. Last Tuesday at 3:14 AM, my PagerDuty went off. Usually, this means a Cloud Run instance is OOMing or a database connection pool has saturated. This time, it was a billing alert from Google Cloud. My Gemini API usage had spiked by 1,400% in ninety minutes. Someone had found a way to bypass my "viewer" role restrictions and was proxying high-token-count reasoning requests through my automation backend. By the time I killed the service, I was out $400. The culprit wasn't a sophisticated zero-day. It was a classic logic flaw in how I handled FastAPI dependencies. I had implemented authentication—I knew who the user was—but my authorization logic was leaky. I was checking if a...

Architecting Robust AI Agents: Why Linear Chains Fail in Production

Robust AI agents are built using state-machine architectures to provide granular control over non-deterministic LLM outputs and prevent recursive cost spikes. By replacing linear chains with directed graphs, developers can implement explicit error routing, checkpointing, and human-in-the-loop validation for production-grade reliability. It was 3:14 AM on a Tuesday when my PagerDuty finally screamed loud enough to wake my spouse. I stumbled to my desk, squinting at a Grafana dashboard that showed a vertical line in my Gemini API token usage. My "simple" AI agent, designed to categorize customer support tickets and draft responses, had entered a recursive death loop. In less than twenty minutes, it had burned through $480 in API credits and was showing no signs of stopping. The culprit? A linear chain. I had built a sequence of calls where the output of Step A fed into Step B. When Step B received a slightly malformed JSON object from the model—a common occurrence when deal...

Replacing Celery with FastAPI Background Tasks for AI Automation

FastAPI Background Tasks provide a lightweight alternative to Celery by executing functions after a response is sent within the same process. This migration can reduce cloud infrastructure costs by over 70% for small-scale AI automation workflows on Google Cloud Run. I was staring at my Google Cloud Billing console at 2:14 AM on a Tuesday when I realized I had built a monster. My "simple" AI automation agent, which was supposed to handle a few hundred PDF processing tasks a day, was costing me $422 a month just in infrastructure overhead. Most of that wasn't even the LLM tokens—it was the managed Redis instance and the three extra Cloud Run services I had spun up to act as Celery workers. Even worse, I was dealing with a recurring "Ghost Task" bug where tasks would re-run indefinitely because the worker's visibility timeout was shorter than the Gemini API's response time during a peak period. I didn't need a distributed task queue. I needed a way...

FastAPI Dependency Injection: Scaling Complex AI Agent Architectures

FastAPI Dependency Injection allows developers to manage complex object lifecycles and shared state by decoupling resource creation from business logic. By using class-based providers and yield-based cleanup, you can eliminate resource leaks and simplify unit testing in asynchronous AI architectures. I remember the exact moment I realized my FastAPI architecture was failing. It was 3:14 AM on a Tuesday, and my monitoring dashboard for TechFrontier's document analysis service was bleeding red. We were seeing a 14% failure rate on long-running AI inference tasks. The error message in the logs was a cryptic RuntimeError: Task attached to a different loop followed by a cascade of sqlalchemy.exc.ResourceClosedError . At the time, I was building a complex pipeline that integrated the Gemini API for multi-modal analysis. To keep things "simple," I was passing database sessions and API clients directly through five or six layers of function calls. My route handlers had becom...

Scaling FastAPI WebSockets for Real-time AI Monitoring

Scaling FastAPI WebSockets on serverless infrastructure requires a centralized connection manager and a Redis Pub/Sub broker to handle state across multiple instances. By implementing heartbeat mechanisms and pruning zombie connections, developers can maintain 99.9% stability and prevent memory leaks in real-time AI monitoring dashboards. Last Tuesday at 10:15 AM, my monitoring dashboard for a fleet of autonomous AI agents went dark. I was in the middle of a live demo for a potential client, showing off how our agents use the Gemini API to process multi-modal data in real-time. Everything looked perfect for the first five minutes, and then the graphs stopped updating. The browser console was a sea of red WebSocket connection to 'wss://api.techfrontier.blog/ws/agents' failed: Error during WebSocket handshake: Unexpected response code: 504 errors. It was embarrassing, but more importantly, it was a signal that my "simple" WebSocket implementation was fundamentally br...

Building a Resilient Gemini API Multi-Agent Workflow in Python

A resilient Gemini API multi-agent workflow is built by replacing linear chains with state-machine architectures and enforcing structured JSON outputs. This approach reduces context pollution and prevents infinite loops by isolating agent states and using Gemini 1.5 Flash for validation tasks. Last Tuesday, my cloud bill did something I hadn’t seen since the early days of experimental crypto mining. In just four hours, my development environment racked up $420 in API costs. The culprit? A recursive loop between two Gemini 1.5 Pro agents that were "politely" arguing over the formatting of a JSON object. One agent would output a minor syntax error, the second would attempt to correct it but hallucinate a new field, and the first would then try to "fix" that new field, ad infinitum. I was building what I thought was a straightforward content transformation pipeline. The goal was to take raw engineering specifications, have one agent summarize them, a second agent g...