Building an AI Agent

Note: This post is predominantly for my own understanding and for anyone starting from scratch.

After attending a fantastic masterclass delivered by one of Tech leads, I came straight to my computer to work on a couple of agents we were tasked with building. However, I always like to put things down in written just so that I can understand them better.

Agents have 3 mandatory components which come together to get stuff done:

  1. The LLM brain: in my case ChatGPTm the role for gpt is to function as a reasoning engine: Assess the question, issue commands - or actions - and then respond in a human readable manner.

  2. The tools: a list of python functions decorated with @tool - something like get_current_time. The tools play a role that the LLM can’t, things like looking up current data, running calculations or accessing a database. The LLM relies on the input it gets from the user to know what tool to use and when to use it. Other tools could be calculators, web browsers or a tool to run database queries.

  3. The prompt: this is where LangChain comes in with a prompt template pulled from the LangChain hub. This is the most essential bit on the ReAct loop. The prompt passed to the LLM says what is the LLM role (a helpful assistant…), the tool it has to work and the format needed for the answer. The Prompt is the Standard Operating Procedure (SOP). It forces the LLM to think clearly, use the available resources correctly, and output its thought process in a format the AgentExecutor can understand and parse.

My aim was to build a financial services agent - made out of 4 smaller agents - that would sequentially get the work done. The work is implemented in Python, explicitly using the design pattern for calling the Gemini API

The agent roles are

  • Agent 1 - Intent Analyst

    • Context & Routing: Analyzes the customer query and classifies it (e.g., 'Debt Management', 'Retirement Planning', 'Boundary/Advice Seeking'). This determines the focus of the content.

    • Instructions: "You are an expert intent classifier. Classify the user query into one of four categories: [debt, savings/retirements, spending and advice seeking]

  • Agent 2 -Financial Educator

    • Knowledge Retrieval: Gathers relevant, general financial education, frameworks, and concepts using Google Search grounding.

    • Instructions:  You are a neutral financial content generator. Use search results to find general frameworks (e.g., 50/30/20 rule, snowball method, etc.). Do not offer any personal advice.

  • Agent 3 - Financial Coach

    • Drafting the Response: Creates the initial, empathetic, and thought-provoking coaching response based on the Educator's content.

    • Instructions: You are a kind, empathetic financial coach. Your goal is to empower the customer. Ask clarifying questions, use 'I cannot recommend...' if needed, and focus on trade-offs. The tone must be supportive.

  • Agent 4 - Compliance Officer

    • Critical Guardrail: Reviews the Coach's draft response against strict regulatory rules. This is the supremely important  guardrail. If non-compliant, it rewrites the response to be educational and compliant.

    • Instructions: You are a regulatory compliance auditor. Your sole task is to ensure the text contains absolutely NO specific investment, stock, loan, or product recommendations. If it does, rewrite the section into an educational, compliant statement."

The orchestration each agent has to follow is very simple: is based on a five-step sequence for every customer query. This is what I think is a ReAct (Reasoning and Acting) framework.

  • Step 1: Intake - The Customer Query is received.

  • Step 2: Intent Analysis (Agent 1): The Intent Analyst processes the query and sets the topic (e.g., Debt).

  • Step 3: Content Generation (Agent 2 & 3):

    • The Financial Educator generates the necessary educational content (e.g., Debt repayment frameworks).

    • The Financial Coach takes this content and drafts the initial, customer-centric response (Draft A).

  • Step 4: Compliance Audit (Agent 4)

    • The Compliance Officer reviews Draft A.

    • If compliant: Draft A becomes the Final Response.

    • If non-compliant: The Compliance Officer rewrites the problematic parts, creating Draft B (the compliant Final Response).

  • Step 5: Delivery -  The Final Response (Draft A or B) is delivered to the customer.

How the agent works?

  1. Tool Integration: It takes your list of tools and automatically inserts their names and descriptions into the Prompt Template. This is how the LLM "knows" what tools are available.

  2. Input Parsing: It sets up the logic to parse the LLM's raw text response. 

  3. Agent Logic Creation: It combines the LLM and the tools into a single, executable item (a LangChain Runnable), which the AgentExecutor can then use to start and manage the ReAct loop.

On my case, I did not use LangChain, the orchestration component was delivered by my processFinancialQuery() - the main coordinator

  • Sequential Execution: Runs agents 1→2→3→4 in order

  • State Passing: Each agent receives output from previous agents

  • Return Aggregation: Collects all intermediate and final outputs

  • No Loop/Iteration: It's a straight pipeline, not a loop (by design)

  • No Retry Logic: Failures bubble up (could be added)

⚙️ Executor Feature Comparison

Feature LangChain Our Implementation Component
Sequential Execution Built-in loop Explicit async/await chain processFinancialQuery()
Tool Calling Dynamic tool selection Fixed 4-stage pipeline Individual agent functions
State Management Built-in memory Manual state passing Function parameters
Error Handling Retry logic, fallbacks Basic try-catch Route handler
Observability Callbacks, logging Console logs Each layer
Streaming Supported Not implemented N/A
Intermediate Steps Recorded automatically Manually collected processFinancialQuery() return

For tech specs, this is what my agent ends up using:

💻 Tech Stack Architecture

Layer Technology Purpose
Frontend Framework React 18 + Vite Fast dev, HMR, component-based UI
Routing Wouter Lightweight client-side routing
State Management TanStack Query v5 Server state, caching, mutations
UI Components Shadcn + Radix UI Accessible, composable primitives
Styling Tailwind CSS Utility-first, responsive design
Backend Runtime Node.js + Express HTTP server, API routes
AI Provider Gemini 2.5 Flash Intent, education, coaching, compliance
AI Integration Replit AI Integrations Managed API keys, billing
Storage In-Memory Maps Conversations & messages (MVP)
Type Safety TypeScript + Zod Compile-time + runtime validation
Schema Drizzle ORM types PostgreSQL-ready data models

Last but not least: the result can be found here

Cristian Garcia

A man is whatever room he is in

http://www.cristiangarcia.uk
Previous
Previous

My top 5 books for 2025

Next
Next

Los Balcanes, parte 2