arXiv:2602.21257cs.CLcs.DB2026-02

用SQL式语言让大模型更高效管理上下文与调用资源

Structured Prompt Language: Declarative Context Management for LLMs

  • 用声明式语法控制上下文预算和查询优化,像操作数据库一样管理LLM输入
  • 同一脚本在本地运行零成本,在云端仅需0.002美元,成本差异达68倍
  • 支持多模型调度、文档智能分块、自动容错代理流程,适合构建复杂AI系统

我们提出SPL(Structured Prompt Language),一种受SQL启发的声明式语言,将大语言模型视为生成式知识库,其上下文窗口作为受限资源。SPL提供显式的令牌预算/限制管理、自动查询优化器、类似SQL的EXPLAIN分析透明性,以及原生集成检索增强生成(RAG)与持久记忆的统一框架。SPL-flow将SPL扩展为具备三层次提供商回退策略(Ollama → OpenRouter → 自修复重试)的鲁棒智能体流水线,对.spl脚本完全透明。五个扩展展示了该范式的广度:(1) Text2SPL(多语言自然语言转SPL);(2) 模型混合路由(MoM),运行时将每个提示分发至领域专家模型;(3) 逻辑分块,通过现有CTE语法自然处理超上下文长度文档,分解为映射-归约流水线,将注意力成本从O(N²)降至O(N²/k);(4) SPL-flow,具备弹性回退的声明式智能体编排层;(5) BENCHMARK,支持并行多模型对比,自动保存最优结果。我们提供正式EBNF语法,两个可安装的Python包(spl-llm, spl-flow),并与Prompty、DSPy、LMQL对比。SPL平均减少65%提示模板冗余,执行前暴露68倍成本差异,同一.spl脚本可在OpenRouter上以0.002美元运行,或在本地Ollama实例上零边际成本运行,无需修改。

原文摘要 · Abstract (English)

We present SPL (Structured Prompt Language), a declarative SQL-inspired language that treats large language models as generative knowledge bases and their context windows as constrained resources. SPL provides explicit WITH BUDGET/LIMIT token management, an automatic query optimizer, EXPLAIN transparency analogous to SQL's EXPLAIN ANALYZE, and native integration of retrieval-augmented generation (RAG) and persistent memory in a single declarative framework. SPL-flow extends SPL into resilient agentic pipelines with a three-tier provider fallback strategy (Ollama -> OpenRouter -> self-healing retry) fully transparent to the .spl script. Five extensions demonstrate the paradigm's breadth: (1) Text2SPL (multilingual NL->SPL translation); (2) Mixture-of-Models (MoM) routing that dispatches each PROMPT to a domain-specialist model at runtime; (3) Logical Chunking, an intelligent strategy for documents exceeding a single context window--expressed naturally through SPL's existing CTE syntax with no new constructs, decomposing a large query into a Map-Reduce pipeline that reduces attention cost from O(N^2) to O(N^2/k) and runs identically on cloud (parallel) or local hardware (sequential); (4) SPL-flow, a declarative agentic orchestration layer with resilient three-tier provider fallback; and (5) BENCHMARK for parallel multi-model comparison with automatic winner persistence. We provide a formal EBNF grammar, two pip-installable Python packages (spl-llm, spl-flow), and comparison against Prompty, DSPy, and LMQL. SPL reduces prompt boilerplate by 65% on average, surfaces a 68x cost spread across model tiers as a pre-execution signal, and runs the identical .spl script at $0.002 on OpenRouter or at zero marginal cost on a local Ollama instance--without modification.

提示工程代码生成LLM架构智能体

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。