Skip to content

Language models, retrieval, prompting, and language adaptation · Reasoning, decoding, and discrete diffusion language · LNG074 · Algorithm

ReAct reasoning and acting

Interleaves language-model reasoning with actions and observations from external tools or environments. It is an agent prompting/control pattern, not an autonomous guarantee of correct tool use.

Runs in CodeSplash (AI) Component demo, not a trained model CodeSplash's own run

In CodeSplash

A CodeSplash feature runs this entry inside the app, not as a Python and R method. There's no Set up button; open it where the label says. It uses the AI model you set up in Settings › AI, and the project's AI privacy settings decide what it may send.

Do it in CodeSplash Run it on your own data in the app: AI panel (⇧⌘A) › Agent mode, with the AI model you set up in Settings › AI: it plans the steps, you approve the plan (or skip or edit steps), then it alternates tool calls with their results under step, time and cost limits, re-runs deterministic steps to verify, and writes a summary; Replay without a model re-runs the trail. The loop is prompted (no fine-tuning on trajectories).

Availability
Runs in CodeSplash (AI)
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Interleaves language-model reasoning with actions and observations from external tools or environments. It is an agent prompting/control pattern, not an autonomous guarantee of correct tool use.

Question / objective

Can feedback from tools improve a model's multi-step task execution?

Architecture / computational structure

A controller alternates model outputs, parsed actions, tool execution, and observations. The base model may remain frozen; tool schemas, prompts, action budgets, and stopping rules configure the loop.

Assumptions

Tool outputs may be incomplete or adversarial, actions can be invalid, and reasoning text can rationalize mistakes.

Formula / computational definition

at∼πθ(⋅∣ht),ot∼Environment⁡(at),ht+1=[ht;at;ot].a_t\sim\pi_\theta(\cdot\mid h_t),\qquad o_t\sim\operatorname{Environment}(a_t),\qquad h_{t+1}=[h_t;a_t;o_t].

A trajectory alternates internal text/state, action a_t, and observation o_t; the policy conditions on accumulated history h_t. An environment transition supplies o_t after a_t, so the closed loop depends on both model and tool behavior.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Tool schemas, action parser, observation formatting, iteration budget, stopping criteria, prompt examples, and error recovery.

Fitting, fine-tuning, and adaptation

Prompt-based ReAct requires no fine-tuning. Optional trajectory SFT needs vetted action/observation traces, with response masks and tool schemas preserved. Validate on unseen environments, injected tool errors, and capability retention; action permissions and external side effects must be controlled separately from model training.

How to use and calibrate

Assess task success, action validity, source grounding, recovery from tool errors, model/tool-call cost, and failures under misleading observations. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Answer a research question using approved retrieval tools and traceable observations.

Interpretation / pitfalls

Watch out

External side effects require explicit operational controls; the pattern alone supplies none. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: Use a documented model interface plus explicit tool functions and state handling; R can implement the controller natively or bridge Python. The native example shows a deterministic state update from an observation and deliberately performs no external action.

Example scope: ReAct-style trace accumulation using a supplied observation; executes no external tool.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — ReAct-style trace accumulation using a supplied observation; executes no external tool.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
history = [{"type":"question","text":"Find the documented boiling point."}]
history += [{"type":"action","text":"retrieve reference"},{"type":"observation","text":"Reference reports 100 C at 1 atm."}]
print(history[-1])
R

Component demo, not a trained model — ReAct-style trace accumulation using a supplied observation; executes no external tool.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
history <- list(list(type="question",text="Find the documented boiling point."))
history <- c(history,list(list(type="action",text="retrieve reference"),list(type="observation",text="Reference reports 100 C at 1 atm.")))
print(tail(history,1))

Sources

From the CodeSplash reference, entry LNG074.