← Horacio Parisotto — Senior Full-Stack & AI Engineer

Engineering case study

RAG + AI agent in production, with bounded AI costs

By Horacio Parisotto, Senior Full-Stack & AI Engineer · October 2026

This is a breakdown of the AI search system I built for OurBlock, a 24/7 multi-camera security platform I run as solo founder. The goal: let a property guard type a natural-language question about camera events and get a useful answer — without every query hitting an expensive LLM call.

AI providers

4

Test suite

94 tests, 100% pass

AI cost per frame

−94%

The problem with "just use an AI agent for everything"

A naive design routes every user query through an autonomous agent with tool calling. It works, but it's slow and expensive for the 80% of queries that are simple: "show me events from last night" doesn't need multi-step reasoning, it needs a fast similarity search.

The fix was intent routing at the entry point: one natural-language input box, but two very different backends depending on what's actually being asked.

Path 1 — RAG for simple semantic queries

Straightforward queries trigger a RAG search over event embeddings, generated with OpenAI's text-embedding-3-small and stored in sqlite-vec — no separate vector database service to run or pay for. Results come back ranked with per-result similarity scores, and there's no LLM call in this path at all: it's pure vector search, which makes it both fast and nearly free.

Path 2 — a multi-step agent for investigative queries

Complex queries ("did anyone approach the side gate between 2 and 4am and did they leave anything behind?") route to an autonomous multi-step agent built on Anthropic's Claude Haiku 4.5 with tool calling. The agent has four tools available to it:

The agent's reasoning streams to the UI in real time via Server-Sent Events, so the user sees it working through the investigation step by step rather than staring at a spinner.

Keeping agent costs bounded

An autonomous agent with tool calling can, in theory, loop indefinitely. In production, the controls that keep it bounded and cheap were as important as the agent logic itself:

Combined with downscaling frames to 480p and routing by camera tier, these cut the AI cost per analyzed frame by an estimated 94% from list prices, across both the vision pipeline that classifies every motion event and the agent that handles investigative queries.

Testing an agent without paying for every test run

The agent loop, tool dispatch, and vector search are covered by the platform's Vitest suite, 94 tests, all passing, with the Anthropic and OpenAI SDKs mocked. That keeps the suite hermetic — no API keys, no network calls, no per-run cost — while still exercising the actual control flow: tool selection, iteration limits, and how the agent handles a tool returning nothing useful.

Result

A single search box that feels like one system to the user, but is really two systems chosen by intent — cheap vector search for simple lookups, a bounded multi-step agent for real investigations — with the cost controls to keep the whole thing sustainable in production.

I write about production AI systems, cost optimization, and infrastructure. See more case studies on my portfolio →