Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
YouTube Security VideosAndroid Police: Samsung is smashing records! #shorts #tech #phones(21.09.2026 um 13:55 Uhr)
YouTube Security Videosheise & c't: Bundesnetzagentur wollte diesen Futterautomaten verbieten(21.09.2026 um 13:53 Uhr)
YouTube Security VideosNeil Patel: Your Google Traffic Isn't An Asset It's A Loan #shorts(21.09.2026 um 14:05 Uhr)
Windows Tipps & SecurityF-14 A Tomcat Top Gun endlich als Revell Klemmbausteinmodell erhältlich(21.09.2026 um 14:27 Uhr)
Sichere ProgrammierungShow the Hand-Back Sample Before Approving an Agent Score(21.09.2026 um 14:15 Uhr)
Sichere ProgrammierungHybrid retrieval in one Postgres query: RRF over tsvector + pgvector(21.09.2026 um 14:15 Uhr)
YouTube Security VideosAndroid Police: Samsung is smashing records! #shorts #tech #phones(21.09.2026 um 13:55 Uhr)
YouTube Security Videosheise & c't: Bundesnetzagentur wollte diesen Futterautomaten verbieten(21.09.2026 um 13:53 Uhr)
YouTube Security VideosNeil Patel: Your Google Traffic Isn't An Asset It's A Loan #shorts(21.09.2026 um 14:05 Uhr)
Windows Tipps & SecurityF-14 A Tomcat Top Gun endlich als Revell Klemmbausteinmodell erhältlich(21.09.2026 um 14:27 Uhr)
Sichere ProgrammierungShow the Hand-Back Sample Before Approving an Agent Score(21.09.2026 um 14:15 Uhr)
Sichere ProgrammierungHybrid retrieval in one Postgres query: RRF over tsvector + pgvector(21.09.2026 um 14:15 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Agents in 60 lines of python : Part 3

The Agent Loop The entire AI agent stack in 60 lines of Python. You've seen Claude search files, read them, then search again. ChatGPT with Code Interpreter writes code, runs it, sees an error, fixes it, runs again. That…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!




The Agent Loop



The entire AI agent stack in 60 lines of Python.





You've seen Claude search files, read them, then search again. ChatGPT with Code Interpreter writes code, runs it, sees an error, fixes it, runs again. That back-and-forth isn't magic. It's a loop.



Lesson 2's agent made one tool call and stopped. That's fine for "what's 2 + 2?" but useless for anything real. Multi-step tasks — analyzing a codebase, debugging a function, researching a topic — need the agent to keep going until the job is done.



The agent loop is what makes that possible. And it's shockingly simple.









The concept



Real agents loop: call a tool, see the result, decide what's next, repeat until done. The LLM decides when to stop.





The flow is:





  1. Build messages — system prompt + user task


  2. Ask LLM — send everything so far


  3. tool_calls? — the LLM either returns an answer (done) or asks for tools


  4. Execute tool — run it, append the result


  5. Back to Ask LLM — with the growing messages array



That loop-back arrow from "Execute tool" to "Ask LLM" is the entire difference between a toy and a useful agent.









The code: a for loop with a safety limit






async def agent(task, max_turns=5):
messages = [
{"role": "system", "content": "Use tools."},
{"role": "user", "content": task},
]
for turn in range(max_turns):
msg = await ask_llm(messages)
if not msg.get("tool_calls"):
return msg.get("content", "")






for turn in range(max_turns) — that's the whole loop. Each iteration is one "turn": ask the LLM, check if it wants a tool. If not, return the answer. The max_turns safety limit prevents runaway loops (an LLM that keeps calling tools forever).









The dispatch: executing tools



When the LLM does ask for a tool, we run it and feed the result back:




        messages.append(msg)
for tc in msg["tool_calls"]:
name = tc["function"]["name"]
args = json.loads(tc["function"]["arguments"])
result = tools[name](**args)
messages.append({
"role": "tool",
"tool_call_id": tc["id"],
"content": str(result),
})






The tool_call_id links each result to its request. When the LLM asks for two tools at once, this is how it knows which result belongs to which call.



The messages array grows with every turn. That's the agent's memory within a single task.









Watch the cycle





Message in, LLM thinks, tool runs, result feeds back. Around and around until the LLM decides it has enough to answer.



Try "add 3 and 4, then uppercase hello" — the LLM chains two tool calls across multiple turns. Watch the messages array grow.









What changed from Lesson 2



Lesson 2 was a single LLM call: ask once, maybe use a tool, done. That can't handle "search for files, then read the interesting ones, then summarize." The loop is what makes agents actually useful.



This is the entire runtime of LangChain's AgentExecutor, OpenAI's Agents SDK, AutoGen — a while loop over messages. Every agent framework wraps this pattern. Now you've seen it bare.









The full code



Here's the complete agent in ~30 lines:




tools = {"add": lambda a, b: a + b, "upper": lambda text: text.upper()}
TOOL_DEFS = [
{"type": "function", "function": {"name": "add", "description": "Add two numbers",
"parameters": {"type": "object",
"properties": {"a": {"type": "number"}, "b": {"type": "number"}}}}},
{"type": "function", "function": {"name": "upper", "description": "Uppercase text",
"parameters": {"type": "object",
"properties": {"text": {"type": "string"}}}}},
]

async def ask_llm(messages):
resp = await client.chat.completions.create(
model="gpt-4o-mini", messages=messages, tools=TOOL_DEFS
)
return resp.choices[0].message

async def agent(task, max_turns=5):
messages = [
{"role": "system", "content": "Use tools to answer. Be concise."},
{"role": "user", "content": task},
]
for turn in range(max_turns):
msg = await ask_llm(messages)
if not msg.get("tool_calls"):
return msg.get("content", "")
messages.append(msg)
for tc in msg["tool_calls"]:
name = tc["function"]["name"]
args = json.loads(tc["function"]["arguments"])
result = tools[name](**args)
messages.append({
"role": "tool",
"tool_call_id": tc["id"],
"content": str(result),
})
return "Max turns reached"












Try it





Run this code yourself at tinyagents.dev. Lesson 3 has a live sandbox — type a task, watch the loop cycle through the graph in real time.



Next up: Lesson 4 — Conversation. The agent loop handles a single task. But what about back-and-forth dialogue? That's where conversation memory comes in.






This is Lesson 3 of A Tour of Agents — a free interactive course that builds an AI agent from scratch. No frameworks. No abstractions. Just the code.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Agents in 60 lines of python : Part 3

Thematisch verwandte Begriffe: Agents, lines, python, Part · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94097 | A vulnerability was determined in Netcore NBR200V2 1.3.241127.071246. Th…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick