Skip to main content
G E N A I M A S T E R Y B O O T C A M P · K G P T A L K I E
SPECIAL SESSION · 7 SLIDES · BANKING & FINANCE
Loop
Engineering
You already have the loop. Every agent is one. The discipline is
what you wrap around it: where the state lives, what decides the
work is finished, and on whose clock the whole thing repeats.
Loop Engineering State Outside the Model The Verifier create_agent Budgets
Scan to subscribe
KGP TALKIE · GENAI MASTERY BOOTCAMP 2026 Laxmi Kant Tiwari · Loop Engineering
L O O P E N G I N E E R I N G · T H E M E C H A N I S M
You Have Seen This Before 02
LOOKS LIKE the agent loop. ReAct. Tool calling. It is. Loop engineering is what wraps it.
HumanMessage
the question
MODEL
tool calls?
NO, IT IS DONE
AIMessage(content=...)
the answer the customer sees
STOP
YES, IT WANTS A TOOL
AIMessage(tool_calls)
a request, not an action
your code runs it
the model runs nothing
ToolMessage
what the tool returned
appended, and the whole list goes back in
The model never executes
It writes a request and waits. Every guard
you want lives in the gap you own.
Nothing here counts
There is no natural limit in the picture.
If you want one, you have to add it.
Nothing here remembers
Everything the run knows is in that list,
and the list dies with the run.
KGP Talkie · GenAI Mastery Bootcamp · YouTube · LinkedIn · kgptalkie.com Loop Engineering · The Mechanism Slide 02 / 07
L O O P E N G I N E E R I N G · T H E D E F I N I T I O N
Five Steps, and Two of Them Are Not the Model 03
LOOKS LIKE a while loop with extra steps. The state and the check sit outside the model.
Shared state, outside the window
plan.md verdicts.json the ticket
reads
updates
Task
the goal, plus what is known
Act
the model, calling a tool
Observe
what actually came back
Verify
not the model that acted
Decide
go again, or stop
STOP
revise, with the reason
AND IT RUNS AT THREE SPEEDS AT ONCE
INNER · MINUTES
The agent acts, checks, and fixes.
The check is a test that passes or fails.
MIDDLE · HOURS
You read the output and steer it.
The check is your judgement.
OUTER · WEEKS
Customers use it and react.
The check is the number that moved.
KGP Talkie · GenAI Mastery Bootcamp · YouTube · LinkedIn · kgptalkie.com Loop Engineering · The Definition Slide 03 / 07
L O O P E N G I N E E R I N G · S T A T E
If the Memory Is the Transcript, It Is a Chat 04
LOOKS LIKE a database, or a scratch file. That is exactly what it is.
THE SYMPTOM · WHAT GOES UP THE WIRE
turn 1
turn 2
turn 3
turn 4
turn 5
system question tool request tool result
Only the last two blocks are new. The rest is paid for again,
and the useful signal in the pile keeps thinning. That is context rot.
THE FIX · WHERE THE STATE SHOULD LIVE
THE WINDOW
only what this turn needs
WRITES
READS
THE STORE
plan.md
verdicts.json
the ticket
everything the run has learned
THE THREE MOVES
Compaction summarise the run so far, continue from the summary
Offloading push big outputs to a file, keep only the slice you need
Sub-agents hand a messy subtask out, only its clean result returns
Turn nine does not need turn one in the window.
It needs turn one on disk.
KGP Talkie · GenAI Mastery Bootcamp · YouTube · LinkedIn · kgptalkie.com Loop Engineering · State Slide 04 / 07
L O O P E N G I N E E R I N G · T H E C H E C K
The Maker Cannot Grade Itself 05
LOOKS LIKE code review, or an LLM as a judge. The verdict has to be a value, not prose.
SELF-GRADING · THE SHAPE EVERYONE BUILDS FIRST
MODEL
writes it, then reviews it
"Looks correct to me."
The same weights that produced the answer decide
whether the answer is good. It doubles the calls
and moves the verdict least.
A SEPARATE CHECK · WHAT ACTUALLY CLOSES IT
MAKER
does the work
output
CHECKER
never wrote it
passed: false
missing: ["kyc"]
again, with the reason
WHAT MAKES A CHECK WORTH HAVING
1 · A VALUE, NOT PROSE
{"passed": false,
"missing": ["kyc"]}
A test suite, a schema, a type checker.
Something your code can branch on.
2 · NOT THE MAKER
MAKER CHECKER
A small cheap model
beats a large one
grading itself.
Different prompt, different context, no memory of writing it.
3 · IT WILL BE GAMED
passes
correct
Loops learn to satisfy the checker. Re-read what passed.
KGP Talkie · GenAI Mastery Bootcamp · YouTube · LinkedIn · kgptalkie.com Loop Engineering · The Check Slide 05 / 07
L O O P E N G I N E E R I N G · F A I L U R E A N D C O N T R O L
Four Ways It Fails, and What Catches Each 06
PYTHON · LANGCHAIN 1.X · CALL LIMITS
from langchain.agents import create_agent
from langchain.agents.middleware import ModelCallLimitMiddleware
from langgraph.checkpoint.memory import InMemorySaver
agent = create_agent(
model=model,
tools=[get_statement, calculate_emi],
checkpointer=InMemorySaver(), # thread_limit needs one
middleware=[ModelCallLimitMiddleware(
run_limit=5, # this one invocation
thread_limit=20, # the whole conversation
exit_behavior="end", # stop politely, do not raise
)],
)
# a second, independent ceiling, one level down in the runtime
agent.invoke(question, config={"recursion_limit": 12})
# The budget cap and the no-progress check are not in the
# framework. You write those. No-progress is simply: did
# this turn repeat the previous call with the same arguments?
# And none of them know whether the work is finished. They
# only know it has run long enough that something is wrong.
F O U R F A I L U R E S , F O U R B R A K E S
1 · IT NEVER STOPS
The tell: finance notices before engineering does.
CAUGHT BY max iterations, and a budget cap
2 · IT STOPS TOO EARLY
The tell: nobody notices at all.
CAUGHT BY the completion check, and nothing else
3 · IT REPEATS ITSELF
The tell: your tool returned a shrug, not a value.
CAUGHT BY no-progress detection
4 · IT LEARNS TO PASS THE CHECK
The tell: the rubric passed, the customer did not.
NO BRAKE you have to re-read what passed
KGP Talkie · GenAI Mastery Bootcamp · YouTube · LinkedIn · kgptalkie.com Loop Engineering · Failure and Control Slide 06 / 07
L O O P E N G I N E E R I N G · W R A P - U P
Four Things to Keep 07
State goes outside
If the run's memory is the
message list, it dies with the
run and it costs more every
turn until it does. A file on
disk fixes both at once.
Something else grades it
A model asked whether it is
happy with its own work will
say yes. The verdict has to
be a value your code can
branch on, not a paragraph.
The clock is yours
Minutes, hours, weeks. Each
layer needs its own check and
its own audience. A loop with
no cadence is just a person
pressing enter repeatedly.
Budgets are the backstop
Set run_limit and
recursion_limit on day one,
and then log which one fires.
If it is not the check, the
work was never finished.
Can you check the answer without asking the model?
YES, SO IT CAN CLOSE ITSELF
Wire the check in and let the loop run on its own clock.
NO, SO YOU ARE THE CHECK
Keep it short, keep it watched, and cap it hard.
KGP Talkie · GenAI Mastery Bootcamp · YouTube · LinkedIn · kgptalkie.com Loop Engineering · Wrap-up Slide 07 / 07