World Models and Lookahead
Predict the result of an action before doing it; choose the action with the best predicted outcome.
World Models and Lookahead is a free AI Agents lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Predict Before You Act
The best agents don't just try things — they predict outcomes first, choose the best, then act. This is the same idea as searching N moves ahead in chess.
What Is a World Model?
A world model predicts: given current state S and action A, what is the next state S'?
For an LLM agent, the LLM itself can be the world model:
prediction_prompt = '''
Current state: {state}
Proposed action: {action}
What is the most likely outcome? Return JSON: {new_state, success_probability}.
'''Why Lookahead Helps
Some actions are irreversible (sending email, deleting data). Predicting first can avoid expensive mistakes.
Other actions have unclear outcomes. Predicting forces the agent to think about consequences.
Simple 1-Step Lookahead
candidates = [a1, a2, a3]
predictions = [predict(state, a) for a in candidates]
best = max(zip(predictions, candidates), key=lambda p: p[0].success_probability)[1]
act(best)Multi-Step Lookahead (Tree Search)
Expand multiple steps ahead. For each candidate, predict outcomes, then predict outcomes of those outcomes:
def search(state, depth=2):
if depth == 0:
return value(state)
actions = candidate_actions(state)
best_score = -math.inf
for a in actions:
next_state = predict(state, a)
score = search(next_state, depth - 1)
best_score = max(best_score, score)
return best_scoreTree of Thoughts
Yao et al. 2023 — extends CoT into a tree. The agent generates multiple "thought paths", evaluates each, expands the most promising:
import random
def continue_thought(question):
return question + ' -> idea' + str(random.randint(1, 100))
def score(t):
return len(t)
def expand_top_k(thoughts, scores, k):
ranked = sorted(zip(thoughts, scores), key=lambda x: -x[1])
return [t for t, s in ranked[:k]]
def ToT(question):
thoughts = [continue_thought(question) for _ in range(5)]
for depth in range(2):
scores = [score(t) for t in thoughts]
thoughts = expand_top_k(thoughts, scores, k=3)
return thoughts
result = ToT('How to reduce agent latency?')
print('Top thoughts:', result)
Monte Carlo Tree Search (MCTS)
For very large search trees: sample paths, score outcomes, propagate scores back up to inform future selections. Used heavily in game-playing AI; emerging in agent research.
Cost of Lookahead
Each predicted action is an LLM call. Lookahead with depth=2, branch=3 = 9 calls just to decide ONE action. Use lookahead only for high-stakes decisions.
Heuristic Pruning
Skip predictions for obviously bad actions. Pre-filter candidate actions with a cheap classifier or rule.
Calibrating Predictions
LLM predictions are imperfect. Calibrate by occasionally comparing predicted vs actual outcomes:
for trace in recent_traces:
predicted = trace.predicted_outcome
actual = trace.actual_outcome
log.info('prediction-accuracy', match=(predicted == actual))When NOT to Use Lookahead
- Cheap, reversible actions — just try them
- Latency-critical paths
- Tasks where outcomes are obvious
When TO Use Lookahead
- Irreversible actions (financial, communication)
- Multi-step plans where mistakes compound
- High-stakes domains (medical, legal)
- Adversarial tasks (games, debate)
o1-style Internal Lookahead
OpenAI o1 / o3 reasoning models do internal lookahead automatically during the "thinking" phase. They explore many continuations before committing. You don't need to implement it externally.
Lookahead Trade-off
What is the main cost of multi-step lookahead?
Recap
Predict before you act. 1-step lookahead is cheap and useful; tree search is expensive but powerful. Reasoning models (o1, o3) automate this. Use sparingly.
Frequently asked questions
Is the “World Models and Lookahead” lesson free?
Yes — the full text of “World Models and Lookahead” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “World Models and Lookahead”?
Predict the result of an action before doing it; choose the action with the best predicted outcome. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “World Models and Lookahead” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Hierarchical Task Decomposition
- Goal Stacks and Backtracking
- World Models and Lookahead
- Reflection and Self-Critique Loops