モナド的文脈工学
Monadic Context Engineering
December 27, 2025
著者: Yifan Zhang, Mengdi Wang
cs.AI
要旨
大規模言語モデル(LLM)の普及は、複雑な推論とツール利用を可能とする自律エージェントへの移行を促進してきた。しかし、現在のエージェントアーキテクチャは、命令型的でアドホックなパターンを用いて構築されることが多い。これにより、状態管理、エラー処理、並行性における困難に悩まされる脆いシステムが生じている。本論文は、Functor、Applicative Functor、Monadという代数的構造を活用し、エージェント設計に形式的な基盤を提供する新しいアーキテクチャパラダイムであるMonadic Context Engineering(MCE)を提案する。MCEはエージェントのワークフローを計算コンテキストとして扱い、状態伝播、短絡型エラー処理、非同期実行といった横断的関心事を、抽象化の代数的性質によって本質的に管理する。我々は、Monadが如何に堅牢な逐次合成を可能にするか、Applicativeが如何に並列実行のための原則的な構造を提供するか、そして決定的に、Monad Transformerが如何にこれらの能力を体系的に合成することを可能にするかを示す。この階層化されたアプローチにより、開発者は単純で独立検証可能なコンポーネントから複雑で耐障害性が高く効率的なAIエージェントを構築できる。さらに、このフレームワークをメタエージェントの記述へと拡張する。メタエージェントはMCEを生成的オーケストレーションに活用し、メタプログラミングを通じてサブエージェントのワークフローを動的に生成・管理する。プロジェクトページ: https://github.com/yifanzhang-pro/monadic-context-engineering
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. Project Page: https://github.com/yifanzhang-pro/monadic-context-engineering.