Leveraging In-Context Learning for Language Model Agents

Shivanshu Gupta, Sameer Singh, Ashish Sabharwal, Tushar Khot, Ben Bogin

TL;DR

Applying dynamic in-context learning (ICL) to interactive language agents significantly boosts performance, reliability, and efficiency—often rivaling costly fine-tuned agents. By bootstrapping an automated trajectory annotation pipeline and combining whole-task trajectory set-selection with on-the-fly step-level reasoning snippets, this approach dramatically cuts inference overhead while mitigating context recency bias.

What Problem Does This Paper Address?

While Large Language Model (LLM) agents can solve multi-step digital tasks using frameworks like ReAct, they frequently derail on complex, long-horizon workflows. Traditional methods combat this either via rigid zero-shot prompt scaffolds (e.g., Reflexion, PEARL) or task-specific fine-tuning (SFT, DPO, RL). However, training-based approaches are prohibitively expensive for frontier models and inflexible when new tasks or APIs emerge.

In-context learning with dynamic demonstration selection is standard practice for static NLP tasks, but applying it to agents introduces distinct hurdles:

  1. Annotation scarcity: Benchmarks rarely provide full, step-by-step action trajectories for training sets.
  2. Context bloat & inference cost: Full interaction trajectories can span dozens of turns; stuffing multiple trajectories into prompt memory rapidly exceeds token limits and balloons API bills.
  3. Prompt dynamics: Due to LLM "recency bias," static exemplars placed at the top of long prompts often lose influence as the agent acts in the environment.

Methods

The authors introduce an end-to-end strategy for harvesting, selecting, and injecting agent demonstrations:

[Unannotated Tasks] ──> (Iterative LLM Annotation + Verifier) ──> [Demonstration Pool]
                                                                        │
        ┌───────────────────────────────────────────────────────────────┴─────────────────────────┐
        ▼                                                                                         ▼
1. Task-Level Trajectories                                                                2. Step-Level Snippets
   • Selected as a complementary set via Set-BSR                                            • Dynamically retrieved via current step's thought
   • Placed at beginning of prompt                                                          • Injected at the prompt's tail end per step
  • Iterative Trajectory Annotation (Algorithm 1): Rather than hand-crafting traces, the authors bootstrap demonstrations. The LLM attempts training tasks, verified by environment unit tests. Newly solved trajectories are added to a demonstration pool and immediately used as dynamic exemplars to help solve the remaining unsolved tasks over multiple rounds, automatically labeling over 95% of tasks.
  • Demonstration Granularities & Placements:
  • Task-level Trajectories: Full task executions placed before the test prompt. Demonstrations are chosen using Set-BSR (BERTScore-Recall set selection), which selects exemplars jointly to maximize coverage of distinct reasoning patterns rather than picking redundant nearest neighbors.
  • Step-level Snippet Demonstrations: To combat recency bias without inflating context, the agent uses its current step’s thought as a query to retrieve short 1–2 step (thought-action-observation) snippets from the pool. These snippets are appended directly to the end of the prompt for a single decision step with minimal token overhead.
  • Subtask-level Trajectories: Evaluated via a Plan-and-Execute (PnE) architecture that decomposes problems into subtasks.

Datasets

  • AppWorld: An interactive coding benchmark comprising 244 scenarios across 9 simulated daily-use apps (e.g., Gmail, Spotify, Venmo) and 457 APIs.
  • Train/Dev Sets: 147 tasks used to build the demonstration pool.
  • Test-Normal (Test-N): 168 tasks evaluating in-distribution task performance.
  • Test-Challenge (Test-C): 417 tasks with higher complexity involving previously unseen applications.

Key Insights and Ideas

  • Self-generated traces beat human demonstrations: An LLM’s own successfully verified trajectories serve as better exemplars than clean, manually authored trajectories (even random auto-trajectories outperformed fixed human demonstrations on AppWorld).
  • Set selection over top-$K$ ranking: Independent similarity ranking often selects near-duplicate examples. Greedy set selection (Set-BSR) picks complementary demonstrations, resulting in substantial accuracy boosts.
  • Snippets solve the context-cost dilemma: While adding a second full trajectory demonstration increases token consumption by ~40% (~100k tokens/task), step-level snippets injected right before generation offer performance boosts with negligible token overhead.
  • Upfront planning is fragile: Despite consuming fewer tokens per subtask, Plan-and-Execute (PnE) underperformed pure ReAct because static upfront plans failed to adapt to dynamic runtime environment feedback.
  • Demonstrations transfer across model sizes and families: Trajectories annotated by GPT-4o substantially enhanced the performance of smaller models (GPT-4o-mini) and open weights (Llama 3.3 70B).

Key Results

  • Performance Jumps on GPT-4o (Test-N):
  • Task Goal Completion (TGC): Climbs from 33.9% (Zero-shot) to 51.6% (Fixed manual prompt) to 66.7% using Set-BSR[2]—a 32.8-point improvement over zero-shot.
  • Reliability (RTGC): The rate of tasks solved consistently across all runs jumped from 36.3% (Fixed) to 57.1% (Set-BSR[2]).
  • Efficiency: The agent required 47% fewer environment steps compared to zero-shot (11.6 vs. 21.6 steps).
  • Prompted vs. Fine-Tuned Agents:
  • GPT-4o with Set-BSR[2] + snippets reached 66.5% TGC on Test-N and 38.2% on Test-C.
  • This prompted, non-fine-tuned agent outperformed specialized trained 32B models (Qwen-2.5-32B trained via SFT, DPO, PPO, RLOO, and GRPO), trailing only the state-of-the-art LOOP RL framework (71.3%).
  • Smaller Models: For GPT-4o-mini, pairing Set-BSR[2] with snippets improved TGC by 14.3 points over a fixed trajectory baseline while cutting environment steps by 40%. On Llama 3.3 70B, demonstration selection improved TGC from 22.6% to 39.3%.

Limitations and Future Work

  • Single Benchmark Scope: Experiments were exclusively conducted on AppWorld due to execution and API cost constraints.
  • Context Saturation: Performance plateaus beyond two full trajectories (e.g., $k=3$ dropped slightly to 65.8% TGC) due to prompt truncation kicking in.
  • Planning Bottlenecks: The subtask-level approach (PnE) struggled with upfront decomposition errors; future work could explore dynamic, mid-execution re-planning alongside snippet retrieval.

Summary updated

BibTeX
@inproceedings{gupta2025leveraging,
  author = {Shivanshu Gupta and Sameer Singh and Ashish Sabharwal and Tushar Khot and Ben Bogin},
  title = {{Leveraging In-Context Learning for Language Model Agents}},
  booktitle = {MTI-LLM @ NeurIPS},
  year = {2025},
  url = {https://openreview.net/forum?id=BzBqRAbdFC},
}
Figure