L
learnllm
0% Completed
Catalog/Agents & MCP/Beyond Prompting: The ReAct Loop & Function Calling
Interactive34 MinutesFree Preview

Beyond Prompting: The ReAct Loop & Function Calling

Master the Thought-Action-Observation lifecycle, parse tool calls safely, and implement human-in-the-loop checkpoints.

[ ENGINEERING OBJECTIVES ]
  • •Scrub through live Thought-Action-Observation traces with our Agentic Workspace widget
  • •Enforce strict schema validation and error feedback loops to cure malformed JSON calls
  • •Implement deterministic recursion guards and token expenditure circuit breakers
  • •Design human-in-the-loop approval gates for high-impact infrastructure actions

The ReAct Cognitive Cycle & Tool Scrubber

Raw LLMs hallucinate because they lack real-time environment access. The ReAct (Reason + Act) architecture grounds the model: it generates an internal reasoning trace ('Thought'), executes external environment tools ('Action'), inspects the structured output ('Observation'), and repeats until the goal is solved. Test the interactive scrubber below to trace RPC calls, token usage, and human approval gates.

PROTOCOL SEQUENCE

ReAct Agent Cognitive Cycle & Approval Flow

NODE 1
User Prompt
Client Request
NODE 2
LLM Reasoner
Thought & Planning
NODE 3
Tool Registry
Schema Validation
NODE 4
Human Gate
Safety Sign-off
Active JSON-RPC Handshakes:
User Prompt→LLM ReasonerGoal Input
LLM Reasoner→Tool RegistryTool Call (JSON-RPC)
Tool Registry→Human GateCheck Approval
Human Gate→LLM ReasonerObservation Return
INTERACTIVE AGENTIC WORKSPACE

ReAct Cognitive Loop & RPC Trace Scrubber

Cumulative: 0.01s
ReAct Loop Phase 1 of 6
USER INPUTT+00:00.000(12ms)

User Prompt & Instruction Framing

Prompt: 142
Completion: 0
Deploy a high-performance FlashAttention-3 Triton kernel on H100 with TMA multicast enabled and verify FP8 tensor core accumulation.

Production Agent State Engine Implementation Suite

Inspect the production-ready implementation of a hardened ReAct agent. It includes strict Pydantic tool registration, automated error-reflection loops that coach the LLM when JSON schemas fail, and pause/resume human approval gates.

Production Implementation Suite (3 Files)
Production ReAct loop with state serialization, recursion bounds, and budget monitoring47 lines • python
import asyncio
import json
from typing import List, Dict, Any, Optional

class ReActAgent:
    """Robust ReAct runtime with circuit breakers, token budgets, and tool dispatch."""
    def __init__(self, llm_client, tool_registry, max_steps: int = 8, token_budget: int = 4000):
        self.client = llm_client
        self.registry = tool_registry
        self.max_steps = max_steps
        self.token_budget = token_budget

    async def run(self, user_query: str) -> Dict[str, Any]:
        messages = [
            {"role": "system", "content": "You are a precise autonomous engineering agent. Use tools to verify all factual claims before concluding."},
            {"role": "user", "content": user_query}
        ]
        total_tokens = 0

        for step in range(self.max_steps):
            response = await self.client.chat.completions.create(
                model="gpt-4o",
                messages=messages,
                tools=self.registry.get_schemas(),
                tool_choice="auto"
            )
            msg = response.choices[0].message
            messages.append(msg)
            total_tokens += response.usage.total_tokens

            if total_tokens > self.token_budget:
                raise TimeoutError("Token expenditure budget exceeded.")

            # Termination condition: LLM decided it has enough evidence
            if not msg.tool_calls:
                return {"status": "SUCCESS", "answer": msg.content, "steps": step + 1, "tokens": total_tokens}

            # Execute tool invocations
            for call in msg.tool_calls:
                result = await self.registry.dispatch(call.function.name, call.function.arguments)
                messages.append({
                    "role": "tool",
                    "tool_call_id": call.id,
                    "content": json.dumps(result)
                })

        return {"status": "EXHAUSTED", "error": "Maximum reasoning steps reached without convergence."}

Never execute arbitrary agent-generated commands or database mutations without a human approval checkpoint or a sandboxed ephemeral runtime.

Cloud GPU Execution SandboxNVIDIA H100 / A100 SXM

Run this lesson's Triton kernels and multi-file code directly in an ephemeral pre-configured PyTorch 2.5 container.

Ready to mark finished?
Track your technical progress.