Loop Engineering · Lesson 1 · Hinglish
🌐 English version →

Loop Engineering ka Map

Aapke paas loop already hai. Yeh course usse engineer karne ke baare mein hai. Pehle, poora territory dekh lete hain.

Aap yeh cheez already likh chuke ho: ek while loop jo model ko call karta hai, jo tool calls wo maangta hai unhe run karta hai, results wapas feed karta hai, aur dobara chal padta hai. Thorsten Ball ki famous line hai ki ek agent bas “an LLM, a loop, and enough tokens” hai1 — aur wo sahi hai ki skeleton ~300 lines ka boilerplate hai.

Loop engineering wo sab hai jo us skeleton ke kaam karne ke baad hota hai. Ek toy loop aur ek aise agent ke beech ka gap jise aap customer ke saamne rakh sako — wo koi genius ka flash nahi hai. Wo kuch levers hain, har ek deliberately tuned. Yeh lesson un levers ka map hai. Har aage wala lesson ek lever mein zoom karta hai.

Aapka mission Behtar, zyada reliable agents banana. Toh hum loop ko sirf admire nahi karenge — hum us par lage knobs dhoondhenge jo reliability ko move karte hain, aur har ek ko soch-samajh kar ghumana seekhenge.

Ek turn, anatomise kiya hua

Map se pehle, unit fix karte hain. Loop engineering turn par operate karta hai — loop ka ek pass. Ek turn ko haddiyon tak utaaro toh wo har baar wahi chhe moves hote hain:

  1. Context assemble karo — wo exact token payload banao jo model is turn dekhega (instructions + state + history + tool defs).
  2. Model call karo — ek inference. Wo ya toh ek answer return karta hai ya ek-ya-zyada tool calls.
  3. Output parse karo — tool calls “bas structured outputs” hain, jinhe aapke control wala code route karta hai.2
  4. Tools execute karo — unhe real world ke against chalao; results aur errors dono capture karo.
  5. Observation append karo — un results ko wapas state mein fold karo. Yeh wahi ground truth hai jisse agent steer kar paata hai.3
  6. Stop condition check karo — ho gaya? budget khatam? human ko de do? Nahi toh: wapas step 1.

HumanLayer poore apparatus ko nichod kar kehta hai: “prompt + switch statement + context builder + loop.”2 Yeh phrase yaad kar lo — neeche har lever in chaar shabdon mein se kisi ek ko engineer karne ka tareeka hai.

Yeh rahe wahi chhe moves us code ki tarah jo aap already likh chuke ho, spine tak utaar kar. Dhyaan do — koi magic nahi hai, “agent” yahi loop hai:

Chhe moves, ~12 lines mein jo aap pehchaante ho
# Factor 8 — own your control flow: yeh loop HI agent hai
state = init(task)
for turn in range(MAX_TURNS):                  # move 6 · stop: budget guard
    ctx     = build_context(state)             # move 1 · context assemble
    reply   = model(ctx)                       # move 2 · model call
    if reply.is_final:                         # move 6 · stop: goal mil gaya
        return reply.answer
    calls   = parse_tool_calls(reply)          # move 3 · output parse
    results = [run(c) for c in calls]          # move 4 · tools execute (+ errors)
    state   = append(state, results)           # move 5 · observation append
raise BudgetExceeded(state)                    # move 6 · stop: guard trip

Is course ka har lever in lines mein se kisi ek ko engineer karne ka tareeka hai: build_context Lever 02 hai, run aur uske tools Levers 03/04 hain, MAX_TURNS Lever 05 hai, run ke andar error handling Lever 06 hai, aur state Lever 07 hai. Loop ka shape khud Lever 01 hai.

1 Context assemble 2 Model infer 3 Parse tool calls 4 Tools + errors 5 Append observe 6 Stop? goal / budget ↺ done nahi → agla turn done ✓
Ek turn = chhe moves. Loop engineering matlab har box ko tune karna — aur move 6 ka wo arrow jo loop-again vs. stop decide karta hai.

Saat levers

Yeh raha map. Har card ek dimension hai jise aap independently engineer karte ho. Aaj inhe master nahi karoge — inhe dekhna seekhoge, taaki jab agent misbehave kare toh aapko pata ho kaunsa knob pakadna hai.

LEVER 01

Control flow

Agla step kaun decide karta hai — aapka code (workflow) ya model (agent)? Master lever; baaki sab isi se latakte hain.

LEVER 02

Context

Har turn window mein kya aata hai. Anthropic isko curate karna agent performance ka sabse bada factor kehta hai.4

LEVER 03

Tools / ACI

Action space — model kya kar sakta hai, aur uske tools kitne clearly named, typed, aur documented hain.3

LEVER 04

Feedback

Wo ground truth jo loop har turn wapas padhta hai progress aankne ke liye. Feedback nahi → loop guess kar raha hai.3

LEVER 05

Stop & budget

Loop kab khatam hota hai. Turns/tokens par caps — roughly 3–20 steps handoff se pehle, ek common rule.2

LEVER 06

Error recovery

Failures kaise wapas fold hote hain — compacted, dump nahi — taaki loop khud ko correct kare, doob na jaaye.2

LEVER 07

State & ownership

Loop ka state aur control kaun rakhta hai — ideally aap: pause/resume, human-in-the-loop, stateless reducers.2

Wo ek distinction jo poore map ko organise karta hai

Agar ek cheez yaad rakhni ho, toh Lever 01 yaad rakho. Anthropic line saaf khींchta hai:

Workflows are systems where LLMs and tools are orchestrated through predefined code paths.
Agents are systems where LLMs dynamically direct their own processes and tool usage, maintaining control over how they accomplish tasks. — Anthropic, Building Effective Agents

Yeh ek baar chunne waali binary nahi hai — yeh har decision par ek dial hai. Zyaadatar production “agents” dial ke workflow wale end ki taraf hote hain: zyaadatar ordinary, deterministic software, jisme model kuch high-leverage decision points par rakha hota hai.2 Anthropic ki blunt advice: “add complexity only when it demonstrably improves outcomes.”3 Paanch named workflow patterns — chaining, routing, parallelization, orchestrator–workers, evaluator–optimizer — sab isi dial ke workflow side par points hain. Inke liye hum ek poora lesson denge.

Aaj ki aapki win

Ab aap do cheezein kar sakte ho jo ek ghante pehle name bhi nahi kar paate the: (1) kisi bhi agent ko chhe-move turn aur saat levers mein decompose karna, aur (2) kisi bhi design point ko workflow (aapka code decide karta hai) ya agent (model decide karta hai) classify karna. Yeh vocabulary aage aane waale sab kuch ke liye aapka diagnostic kit hai. Neeche isse lock karo.

Recall check

Dobara mat padho — retrieve karo. Effortful recall hi is map ko aisi memory mein badalta hai jo agle hafte tak rahegi. Apne dimaag se answer do; feedback turant milta hai.

Primary source — yeh aage padho

Anthropic — Building Effective Agents. Is territory ka sabse high-trust map: workflow/agent split, paanch patterns, aur “start simple” discipline. ~20 minute. Yeh poore course ki backbone hai.

Main aapka teacher hoon — use karo. Map par kuch dhundhla hai? Use kisi aise agent ke against test karna chahte ho jo aapne banaya hai (aapka TradingAgents loop, maan lo)? Mujhe bolo ki batau ki saat levers mein se wo kis par sabse weak hai. Bas chat mein apna sawaal type karo.
📖 Glossary (English) Aage → Lesson 2: The Dial & paanch patterns

Sources

  1. Thorsten Ball — How to Build an Agent (ampcode.com). “An LLM, a loop, and enough tokens.”
  2. HumanLayer — 12-Factor Agents. Factors 8 (own control flow), 9 (compact errors), 10 (small agents, 3–20 steps).
  3. Anthropic — Effective Context Engineering for AI Agents.