Use action and observation to revise the next decision
How to study this lesson
Read one section, trace the worked example, and try the knowledge check. Bookmark saves a shortcut; notes appear in My notebook. Mark lesson complete is your own assessment and does not mark its lab passed.
By the end, you can
- Trace an action-observation loop and its state updates.
- Separate tool failure from negative task evidence.
- Bound revisions with repeated-state and budget checks.
Why observation changes the plan
ReAct studied interleaving reasoning and actions so external observations can influence subsequent decisions. The engineering lesson is to close the feedback loop: choose a bounded action, inspect its result, update explicit task state, and decide what to do next. The course uses public action records and concise decision summaries. It does not require hidden model reasoning traces to make execution observable.
Compare two incident assistants. One writes a complete diagnosis before reading logs and then searches for supporting snippets. The other checks a specific hypothesis, sees that its predicted symptom is absent, and chooses the next discriminating query. The second approach can still fail, but its state updates expose the relationship between evidence and action. That relationship is what the runtime and evaluator can inspect.
Store the observation without overinterpreting it
An observation should retain source, operation, arguments, timestamp, status, and returned value. Separate what the tool reported from what the agent infers. Search returned no records is an observation. The event never happened is an inference that also depends on search coverage, retention, filters, and permissions. Conflating them lets a retrieval failure become a false factual conclusion.
In a deployment investigation, the tool can return not_found, permission_denied, or timeout. Only the first is evidence about the searched dataset, and even that evidence is bounded by the query's scope. A timeout should update service availability state, not the belief that there was no deployment. Model these outcomes explicitly so the next decision can repair access, retry a safe read, narrow the query, or stop with an unresolved question.
A worked diagnostic loop
A service returns errors after a configuration change. The first action checks the latest deployment and finds version 12. The second checks health for version 12 and finds an unhealthy result. The third reads its configuration difference and discovers a missing timeout value. At each step, the next action consumes a concrete observation. If health were normal, the configuration hypothesis would need a different priority.
The demo reduces this process to finding an item in two data sources. Source one returns no match; source two returns the requested record. The explicit state contains attempted sources, observations, and a found value. A deterministic policy chooses the next untried source. This is a simulation of the control loop, not a claim that a few if-statements reproduce a language model's reasoning abilities.
Avoid loops that merely look busy
A loop needs an overall budget, a maximum number of actions, and a progress criterion. Repeating the same query against unchanged data is often no progress, even if the model writes a new explanation. Track a normalized action fingerprint and relevant state version. Repetition can be appropriate after new data or a transient fault, so do not ban every repeated tool name indiscriminately.
Suppose an agent spends three calls retrying a permission-denied lookup. No additional waiting will expand the credential's scope. A bounded controller should classify the failure as requiring a different authorization path or an explicit limitation. By contrast, a single transient timeout on a read-only endpoint may warrant one bounded retry. Recovery should follow error semantics, not the emotional confidence of the next generated sentence.
Evaluate decisions and task outcomes separately
A task can fail despite sensible actions, for example when every permitted source is unavailable. A task can also succeed through a brittle shortcut that would fail on nearby cases. Evaluate final correctness, evidence quality, action appropriateness, cost, and boundary compliance separately. This makes it possible to improve the controller even when the external environment is unreliable.
Construct paired scenarios that differ in one observation: found versus absent, authorized versus denied, fresh versus stale. The next decision should change when the distinction matters. Keep the action vocabulary small enough that each transition has a clear meaning. As capability grows, add specialized policies and verifiers around the loop. More free-form text is not inherently a better state representation, especially when the application needs to resume interrupted work accurately.
Work through the code
Two deterministic stores simulate an action-observation loop. The controller tries each source once and stops when it has a value. Add a separate timeout outcome to practice keeping service failure distinct from absence of a record.
sources = {"cache": {}, "catalog": {"A": {"available": 7}}}
state = {"tried": [], "observations": [], "found": None}
def choose(state):
for source in sources:
if source not in state["tried"]:
return source
return None
for _ in range(3):
if state["found"] is not None:
break
action = choose(state)
if action is None:
break
value = sources[action].get("A")
state["tried"].append(action)
state["observations"].append((action, "found" if value else "not_found"))
if value is not None:
state["found"] = value
print(state["observations"])
print(state["found"])
[('cache', 'not_found'), ('catalog', 'found')]
{'available': 7}Run Python snippets locally with the prerequisites named above. The module coding lab runs directly in your browser.
Pause and reason
A document search returns permission_denied. The agent concludes the document does not exist and searches the same restricted collection again. Identify both errors and propose the next state.
Check your understanding
Which repeated action is most justified?
Your notes
Explain the mechanism in your own words. Add a failure you want to test.