A project to learn about creating a memory aware AI agent.
The basic loop follows this repeating sequence
- INPUT handling - User Input
- Memory RETRIEVAL - SQL/vector databases
- PROMPT BUILDER - CONTEXT assembly
- LLM: Local or cloud
- TOOL USE - decision and execution inner loop (optional)
- STORE Memory - write-back to databases
- OUTPUT - User gets the response
---
title: DETAILED AGENT LOOP
config:
htmlLabels: false
---
flowchart LR
user(("👤 User"))
input(["➡️ INPUT"])
%% MEMORY LAYERS
sql[("🗄️ SQL Memory
(user profile, skills)
")]
vector[("🧠 Vector Memory
(past interactions)
")]
%% RETRIEVAL
retrieval["🔍 RETRIEVAL
(hybrid: SQL + vector)
"]
%% SHORT TERM MEMORY
history[("🧾 Short-term History
(recent messages)
")]
%% CONTROL / STATE
control[("🎛️ Agent State
(current situation, intent, mode)
")]
%% PROMPT BUILDER
prompt["📦 PROMPT BUILDER
(context assembly)
"]
system["📜 System Instructions"]
toolsDef["🧰 Tool Definitions"]
%% LLM
llm["🧠 LLM"]
%% TOOL USE
decision{"Use Tool?"}
tool["🔨 Tool Execution"]
%% MEMORY WRITE
store["💾 MEMORY WRITE"]
%% OUTPUT
output(["📝 OUTPUT"])
%% FLOW
user --> input --> retrieval
retrieval --> sql
retrieval --> vector
retrieval --> prompt
input --> prompt
history --> prompt
control --> prompt
system --> prompt
toolsDef --> prompt
prompt --> llm
llm --> decision
decision -->|yes| tool --> llm
decision -->|no| store
llm --> store
store --> sql
store --> vector
store --> history
store --> output
This is where everything comes together:
- user input
- retrieved memory (RAG)
- system instructions
- agent state
- tools
Structured, short-lived data that controls what the agent is trying to do right now. This prevents the LLM from drifting.
It is NOT:
- long-term memory (that’s your DBs)
- the LLM itself
- just chat history
It IS
- the current situation
- intent
- mode of operation
SQL
- structured facts
- skill tracking
Vector
- semantic recall
- past conversations
- tools are defined in prompt
- LLM decides whether to call them
- system executes them
- result goes back into LLM
Everything funnels through:
- one place that decides what to store
- updates:
- SQL
- vector DB
- short-term history
Agent State
- short-lived
- session-level
- controls behavior
Memory (SQL/vector)
- long-term
- persistent
- stores facts/history