GO TO A SECTION

↑ ↓ to navigate · Enter to openExplore all sections →
FDEInterviews logo
arXiv preprint

TextGrad: Automatic "Differentiation" via Text

Backpropagation with words: an LLM critiques each step of a pipeline, critiques flow backward through the graph, and an LLM rewrites prompts, code or answers. A PyTorch-style API, uneven evidence.

Breakdown by FDE Interviews · 13 min read · Updated

About the original · 2024
AUTHORS
Yuksekgonul, Bianchi, Boen, Liu, Huang, Guestrin, Zou
FROM
Stanford University, Chan Zuckerberg Biohub
PUBLISHED
Jun 2024 · arXiv preprint
CITED BY
270Semantic Scholar · Oct 2026
IN THIS BREAKDOWN · 10 SECTIONS

You can read 18% of this breakdown.

01

The paper in one minute

Neural networks became easy to train once automatic differentiation made backpropagation turnkey: define the computation, call backward(), step the optimizer. Mert Yuksekgonul, James Zou and colleagues at Stanford asked what the equivalent would be for systems built from LLM calls and tools, where nothing is differentiable. Their answer, TextGrad, uses "gradients" that are critiques written by an LLM.

Take the simplest system: a prompt produces a prediction, and an evaluator LLM judges the prediction. In the backward pass, an LLM reads the evaluation and writes feedback on the prediction ("this can be improved by..."). Then, given the prompt, the prediction and that feedback, it writes feedback on the prompt. Textual gradient descent asks an LLM to rewrite the prompt using the feedback. In a larger graph the same happens edge by edge: each variable collects critiques from everywhere it was used, at one extra LLM call per edge per iteration.

The framework copies PyTorch's shape: tg.Variable with requires_grad, a loss, loss.backward(), optimizer.step(), minibatches through tg.sum, plus analogs of momentum (the optimizer sees earlier versions) and constraints (format rules written in English). The objective can be an LLM judge, unit-test results or a simulator's output.

The paper applies it to two kinds of problem. Instance optimization improves one artifact: code for LeetCode Hard problems, answers to GPQA and MMLU questions, small molecules scored by docking and druglikeness, and radiotherapy plans for five prostate cancer patients. Prompt optimization improves one prompt for a task: GPT-3.5 Turbo prompts for Object Counting, Word Sorting and GSM8K, with GPT-4o writing the feedback.

THE IDEA, VISUALLY
BACKPROPAGATING TEXT Prompt variable to improve Prediction LLM(prompt, q) Evaluation LLM judges in words forward ∂L/∂prediction ∂L/∂prompt Each “gradient” is an LLM's critique: “this prediction can be improved by …”, then “so the prompt should …”. One extra call per edge in the graph, every iteration. Textual gradient descent LLM(variable + criticisms) → improved variable THE PYTORCH-SHAPED API tg.Variable(..., requires_grad=True) the prompt, code or answer loss.backward() collect critiques along the graph optimizer.step() rewrite each variable tg.sum(losses) minibatch: concatenate feedback Instance optimization improves one answer; prompt optimization one prompt for a task. IMPROVING ANSWERS AT TEST TIME, GPT-4O LeetCode Hard, completion Reflexion 31 26 36 GPQA best reported 53.6 51 55 MMLU machine learning 85.7 88.4 MMLU college physics 91.2 95.1 OPTIMIZING A PROMPT FOR GPT-3.5 TURBO Object Counting (100 test) 77.8 84.9 91.9 Word Sorting (100 test) 76.7 79.8 79.8 GSM8K (1,319 test) 72.9 81.1 81.1 baseline (CoT) DSPy, 8 examples TextGrad Top panel: grey is the zero-shot or CoT baseline, blue TextGrad.
TextGrad treats a pipeline as a computation graph and replaces numeric gradients with critiques written by an LLM, passed backward one edge at a time. It improved code, answers and prompts, but several gains are small, and the headline question-answering result also uses a majority vote.
Research notes: the problem, approach and results
36%

LeetCode Hard completion with GPT-4o after TextGrad, against 31% with Reflexion and 26% zero-shot (5 seeds)

51 → 55%

GPQA accuracy with GPT-4o, using three refinement passes and a majority vote over the four answers

72.9 → 81.1

GSM8K accuracy for GPT-3.5 Turbo after 12 prompt updates with batches of 3, the same as DSPy's 8-shot prompt

THE PROBLEM

Compound AI systems chain LLM calls, tools and simulators, none of them differentiable. They were tuned by hand, prompt by prompt, with no general way to say which component should change and how.

THE IDEA

Represent the system as a graph of variables (prompts, code, answers, molecules). An LLM writes natural-language feedback on the output, then on each upstream variable given the feedback below it, and an optimizer LLM rewrites each variable. The API mirrors PyTorch.

THE RESULT

GPT-4o's LeetCode Hard completion rose from 26% to 36%, GPQA accuracy from 51% to 55%, and GPT-3.5 prompts tuned with GPT-4o feedback matched or beat DSPy few-shot prompts. Molecule and radiotherapy results are in-silico proofs of concept.

02

Why it matters to an FDE

The useful idea is credit assignment. When a customer's four-step pipeline gives a bad answer, the question is which step to change. TextGrad's backward pass asks an LLM that at every edge, which is a structured way to turn one failed output into feedback on the retrieval prompt, the drafting prompt or the formatting step.

Use a stronger model to tune a cheaper one. The prompt experiments used GPT-4o to write feedback for GPT-3.5 Turbo's prompt. Paying once for a strong model's critique and then serving the cheap model is a sound cost pattern.

Count the calls. Every iteration adds one critique call per edge, plus an update call per variable, plus the evaluator. On a long pipeline, that is several times the cost of a forward pass.

Demand a compute-matched baseline. Refining an answer three times and voting over four versions spends four answers' worth of compute. The fair comparison is four independent answers, voted. The paper does not report it, and our simulation below shows why it matters.

Gate every update on held-out data. In the prompt experiments, TextGrad kept a new prompt only if validation accuracy improved. Without that gate, an LLM's confident critique can make things worse.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 99 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦2 results tables
  • ?5 interview questions with answers
  1. 03Key takeaways

    The operators, the two problem types, the results, and misreadings to avoid

    Free account
  2. 04How it works

    The forward and backward passes, the optimizer, batching, constraints, and each application's loss

    Premium
  3. 05The numbers that matter

    The size of each gain, the inconsistencies, and what is compute-matched and what is not

    Premium
  4. 06Try it yourself

    Is a refinement gain from the critique or from voting? A simulation with a compute-matched baseline

    Premium
  5. 07Where it breaks

    Where textual gradients mislead, cost too much, or are not yet evidence

    Premium
  6. 08Using it on a deployment

    Applying backward critiques to a customer pipeline with gates and budgets

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →