Skip to content
cezar.garcia
All projects

Technical challenge · 2026

Agente de atendimento

A support agent where the model talks and the code decides and executes.

TypeScriptNode.jsClaudePostgreSQLReactVitestPlaywrightDocker
704
unit and integration tests
89
evaluation scenarios with the real model
0
cancellations without confirmation in 801 evaluation runs
2.2 s
average time per conversation on Haiku 5.5

Overview

This is my solution to a technical challenge: a text-message support agent that looks up orders and cancels an order only when the rules allow it. The model is Claude Haiku 5.5.

The design principle is the model interprets and talks; the code decides and executes. Even if someone fully manipulates the model, it has no way to cancel without the customer's explicit confirmation or to see someone else's orders.

A web simulator plays the messaging channel and shows what happens behind each turn: the decision path, the guardrails, the tools and the model calls.

Support simulator: the conversation in the centre, test customers on the left and the turn internals on the right
The order shipped between the question and the yes: the API refuses and the agent reports it correctly.

The problem

Cancelling an order is an irreversible action on the customer's money. A language model is non-deterministic and can be manipulated through text. The project's security premise is blunt: the model can be fooled, and the system cannot depend on it not being fooled.

The challenge's rules became guarantees with a clear owner. Only an order in processing can be cancelled, and the rule is an allowlist in the API domain: a new status is born non-cancellable and doesn't even compile without that decision.

Architecture

There are three apps in a TypeScript monorepo: the orders API (cancellation rules, idempotency, audit and OpenAPI), the agent (webhook channel, orchestration, model, tools and guardrails) and the messaging-channel simulator.

The agent is a single model with four tools and an explicit loop of about 150 lines, with no framework, no multi-agent setup and no RAG. For two operations in a narrow domain, the simplest thing that works keeps the control points visible and testable.

Architecture diagram: simulated messaging channel, agent service with the model, orders API, external catalogue and PostgreSQL
The agent sits between the channel, the model and the orders API.

Confirmation decided by code

The model has no tool that cancels. The only thing it can do is request the cancellation, which creates a pending action in the database and ends the turn with a fixed question.

On the next turn, the yes or no is interpreted by code, without calling the model. The confirmation is bound to a specific order, has an expiry and is valid once. The API revalidates the rule in a transaction with a row lock and executes.

The cancellation-done message is a fixed text, sent only after the API confirms. If the API fails, the outcome is could not confirm, never cancelled, and the same idempotency key allows a retry without cancelling twice.

Sequence diagram of the cancellation between customer, agent, model and orders API, over two turns, with the failure path
Cancelling takes two turns, and the second one never goes through the model.

Defence in depth

Every relevant protection has a layer that does not depend on the model. The prompt is the first line and the weakest. Below it come least-privilege tools, confirmation by code, the rule validated in the API and a check on the answer before it is sent.

Threats are mapped to the OWASP Top 10 for LLM Applications and the OWASP API Security Top 10, each with its mitigation and the test that proves it.

  • Input: personal data is masked before the model; manipulation signals are logged and passed to the model through a separate channel.
  • Output: a cancellation claim the API did not confirm is blocked, and so are refund promises and actions the agent has no way to perform.
  • Prompt leakage: a secret canary and detection of literal chunks of the instructions block the answer.
  • Other people's data: tools take no customer parameter; identity comes from the phone number verified by the channel and the API returns an identical 404 for someone else's order.
  • External catalogue: product text is validated, sanitised and treated as data, never as instructions.
Defence-in-depth diagram: five threats and the barriers in series each one has to cross
Every threat has to cross several barriers, and only the first one is the prompt.

Tests and evaluation with the real model

There are 704 unit and integration tests, with a real PostgreSQL and concurrency. In 67 of them, a scripted model gets things wrong on purpose to prove the harness enforces the rules anyway. Another 21 end-to-end tests bring up the full stack, through a signed webhook and the browser.

The evaluation suite runs 89 scenarios in the real harness, three times each, with a judge and red-team cases. A scenario only counts as passed if it passes all three runs.

In the final round with Haiku 5.5, 98% of the runs passed, at US$ 0.00029 per conversation. Across 801 runs, none had a cancellation without confirmation, a false cancellation claim, leaked instructions or another customer's data.

Decisions and what they cost

Every decision is recorded with its context, the choice and the cost. The main one has a cost I accepted: three messages (the confirmation question, the success and the execution failure) are fixed and sound less natural. It is a deliberate trade, because those three have to be exact.

The output guardrail has a cost too: in the evaluations, most failures were correct refusals by the model that the guardrail blocked out of overcaution. Each case became a test and was fixed.

Full stack

Agent and API
Node.js 22TypeScript 6 (estrito)Express 5Zod 4@anthropic-ai/sdk
Data
PostgreSQL 17Kysely
Simulator
React 19ViteTailwind CSS v4
Testing and evaluation
VitestTestcontainersPlaywrightevals com juiz (LLM)
Operations
Docker ComposeGitHub Actionspino (logs estruturados)OpenAPI 3.1