NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Sharing State Between Prompts and Programs

Ellie Y Cheng, Logan Weber, Tian Jin, Michael Carbin

The Problem: Isolated Program State

Currently, when developers integrate Large Language Models (LLMs) into software, the models are treated as isolated "black boxes." To get an LLM to interact with application data, programmers must manually serialize the program's state into a string (such as a massive JSON blob), send it to the LLM, and then reify (convert back) the LLM's text response into usable program objects.

This "isolated program state" approach suffers from three major flaws:

  1. Verbosity: Developers spend excessive time writing "glue code" for data transfer rather than focusing on core logic.
  2. Scaling Issues: If a data structure, like a large graph, exceeds the LLM's context window, serialization simply fails.
  3. Brittleness: LLMs often hallucinate when forced to generate entire updated data structures from scratch, rather than performing surgical, targeted updates.

The Solution: NIGHTJAR and Shared Program State

To address these limitations, researchers propose a new programming abstraction called Shared Program State, implemented in a system named NIGHTJAR.

NIGHTJAR allows LLM prompts to directly read and write a program's variables, manipulate its heap objects, and even trigger control flow (like break or return). By treating prompts as first-class code with direct access to the host environment, the system drastically reduces boilerplate and improves task accuracy.

Methods: The Natural Function Interface

The core technical innovation enabling this is the Natural Function Interface (NFI). Inspired by the concept of algebraic effects and handlers, NIGHTJAR treats a prompt not as a single, static function call, but as an agentic evaluation.

Architecture of NIGHTJAR showing the interaction between the Python Interpreter, LLM, and Handler via effects like Lookup and Goto.
  • Effects: When an LLM evaluates a prompt, it can emit "effects"—requests to the host system to Lookup a variable, Assign a value, Eval a Python expression, or Goto a specific program label.
  • Handlers: The NIGHTJAR runtime acts as a handler. It catches these effects, executes them within the Python environment, and returns the result to the LLM so it can continue its reasoning.

This architecture enables pass-by-reference instead of pass-by-copy. The LLM doesn't need the entire database loaded into its context window; it can simply "ask" for specific attributes of an object exactly when it needs them.

Key Insights

  • Prompts as First-Class Code: In NIGHTJAR, a prompt is demarcated by a triple-quoted string with a prompt identifier. It is an executable block that "lives" inside the program's scope, rather than just a static string.
  • Shared Control State: A prompt can decide to break out of a loop or return from a function in the host Python program. This elevates the LLM from a passive data processor to an active participant in the program's control flow.
  • Adaptive Querying: The authors discovered that accuracy improved because the LLM could adaptively query the state. In manual implementations, if a programmer forgets to include a specific variable in the JSON dump, the LLM fails. In NIGHTJAR, the LLM simply issues a Lookup effect to retrieve the missing information.

Key Results: Accuracy vs. Performance

The researchers evaluated NIGHTJAR using SPSBench, a custom suite of 25 programs testing complex prompt-program interactions (like graph manipulation and dynamic closures), alongside the standard GSM8K math reasoning dataset.

  • Conciseness: NIGHTJAR programs reduced lines of code (LoC) by an average of 39.6% compared to manual implementations.
  • Accuracy: Task accuracy increased by 4% to 19% across different models (including GPT-4 and Claude 3 Sonnet).
Table comparing Manual Implementation and NIGHTJAR, highlighting NIGHTJAR's higher pass rate at the cost of increased execution time.

However, this powerful abstraction comes with a performance trade-off. Because the LLM acts as an agent making multiple "tool calls" to inspect the state, runtime overhead ranged from 0.4x to 4.3x that of manual, single-shot implementations. Optimizations like eager variable loading and caching were shown to help mitigate this latency.

Limitations and Future Work

While promising, shared program state introduces new challenges:

  • Security and Safety: Giving an LLM direct access to the program heap and control flow is a significant security risk. While basic "allow-lists" for variables were implemented, robust memory isolation and safety mechanisms are required for production use.
  • Latency: The "round-trip" nature of the NFI (LLM $\leftrightarrow$ Handler) is inherently slower than single-shot generation. Future work may explore "bytecode optimizations" for prompts.
  • Non-Determinism: Even at a temperature of 0, LLM executions can be non-deterministic, making debugging shared-state programs more complex than traditional software.

Summary updated

BibTeX
@inproceedings{cheng2026sharing,
  author = {Ellie Y Cheng and Logan Weber and Tian Jin and Michael Carbin},
  title = {{Sharing State Between Prompts and Programs}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=E7ZZRnBQU7},
}
Figure