liveBook · Manning

Agent loop

As explained in Digital Twins in Action

An agent loop is an iterative execution pattern in which an AI agent reasons, invokes tools, updates context or state, and continues until producing an outcome.

An agent loop is a repeated cycle of reasoning, tool execution, and context management. Unlike a single question-and-response exchange with a language model, it lets an agent gather information and take multiple steps toward a decision. section 8.4.3

For home climate control, tools retrieve live solar output, a weather forecast, and indoor conditions. The agent uses those readings to decide whether running the HVAC system would use excess solar power. Tool documentation specifies when a tool is useful and what its inputs and results mean. section 8.4.3

Listing 8.11 Agent with Strands SDK for home climate control
import datetime
from strands import Agent

current_time_str = datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")

system_prompt = f"""  #1
### ROLE
Climate Control Agent. Current Time: {current_time_str}.

### CONSTANTS
- **Target Temp:** 21°C–24°C
- **AC Load:** 1500W
- **Baseload:** 1000W (08:00–16:00); 200W (otherwise).

### CONTROL POLICY
1. **Fetch Data:** Get current solar ($P_{{solar}}$), forecast,
   and indoor temp ($T_{{room}}$ (last 24 hours).
2. **Calculate Excess:** $P_{{excess}} = P_{{solar}} - \\text{{Baseload}}$.
3. **Evaluate State:**
   - **COOLING NEEDED** if $T_{{room}} > 24°C$.
   - **OPPORTUNISTIC COOLING** if $P_{{excess}} > 1500W$ AND $T_{{room}} >
     21°C$ (Priority: Solar).
   - **IDLE** otherwise.
4. **Safety:** Apply hysteresis (don't cycle AC if state changed
   <15 mins ago).
5. **Priorities:** Always prioritize minimizing grid consumption
over comfort.

### OUTPUT INSTRUCTION
Output a concise reasoning step (e.g., "Excess solar 1600W > AC Load;
engaging cooling."), then call `set_aircon_state` if a change is needed.
"""

agent = Agent(  #2
    name="ClimateAgent",
    system_prompt=system_prompt,
    tools=[
        get_realtime_solar_data,
        get_weather_forecast,
        get_space_climate_history,
        set_aircon_state,
    ],
)

The system prompt makes the decision policy explicit: fetch readings, calculate excess power, evaluate cooling conditions, and avoid cycling the AC within 15 minutes of a state change. section 8.4.3

How Digital Twins in Action covers it

How deep it goes
Explains the loop’s role and shows a tool-driven control policy; leaves framework internals to the Strands SDK.
Examples
Uses solar-inverter readings, weather forecasts, indoor conditions, and an HVAC decision policy.
What you can do after
Apply it via the Strands SDK to structure a tool-informed climate-control decision.
How it connects
Links language models, agent tools, APIs, context management, and home digital twins.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Digital Twins in Action. The section links point to the parts of the book it draws on.