arXiv:2512.22431cs.AIcs.CL2025-12

用数学结构设计更稳定的智能体,让状态、错误和并发自动处理。

Monadic Context Engineering

  • 基于函子与单子的代数结构构建智能体架构
  • 单子实现可靠顺序执行,应用函子支持并行任务
  • 适合开发高可靠性智能体系统的研究人员

大语言模型的兴起推动了具备复杂推理与工具使用能力的自主智能体发展。然而,现有智能体架构多采用命令式、临时拼凑的设计模式,导致系统脆弱,难以管理状态、错误与并发。本文提出单子上下文工程(Monadic Context Engineering, MCE),利用函子、应用函子与单子的代数结构,为智能体设计提供形式化基础。MCE将智能体工作流视为计算上下文,通过抽象的代数性质内生地处理状态传播、错误短路与异步执行等横切关注点。我们展示了单子如何实现稳健的顺序组合,应用函子如何提供并行执行的严谨结构,以及单子变换器如何系统性地组合这些能力。该分层方法使开发者能从简单、可独立验证的组件构建复杂、鲁棒且高效的AI智能体。我们进一步将此框架扩展至元智能体,利用MCE实现生成式编排,通过元编程动态创建与管理子智能体工作流。

原文摘要 · Abstract (English)

The proliferation of Large Language Models (LLMs) has catalyzed a shift towards autonomous agents capable of complex reasoning and tool use. However, current agent architectures are frequently constructed using imperative, ad hoc patterns. This results in brittle systems plagued by difficulties in state management, error handling, and concurrency. This paper introduces Monadic Context Engineering (MCE), a novel architectural paradigm leveraging the algebraic structures of Functors, Applicative Functors, and Monads to provide a formal foundation for agent design. MCE treats agent workflows as computational contexts where cross-cutting concerns, such as state propagation, short-circuiting error handling, and asynchronous execution, are managed intrinsically by the algebraic properties of the abstraction. We demonstrate how Monads enable robust sequential composition, how Applicatives provide a principled structure for parallel execution, and crucially, how Monad Transformers allow for the systematic composition of these capabilities. This layered approach enables developers to construct complex, resilient, and efficient AI agents from simple, independently verifiable components. We further extend this framework to describe Meta-Agents, which leverage MCE for generative orchestration, dynamically creating and managing sub-agent workflows through metaprogramming.

智能体架构单子形式化设计

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