How Self-Evolution Works in Long-Running AI Agents: Prompt, Context, and Harness Design

    This article explains how long-running AI agents achieve self-evolution without updating model weights. It breaks the process into four stages—experience collection, structuring, injection, and validation—and shows how to design the three core layers: a versioned Prompt layer, a governed Context layer, and a Harness layer for tools, scheduling, and sandboxing. It also covers migration, observability, and evaluation practices for building a sustainable self-evolution loop.

    1. From Chat Assistants to Long-Running Agents

    Over the past two years, the agent form factor has shifted noticeably. Early solutions were mostly tied to an IDE or a single interface and were essentially conversational shells built around a one-question-one-answer pattern. The newer generation is described as autonomous entities deployed on servers, with cross-session memory, where capability grows the longer they run. The key difference is not model parameters but system architecture: a chat assistant’s boundary is a single request, while a long-running agent’s boundary is a lifecycle.

    Products in this category iterate quickly, and form factors can change within months. Rather than chasing a specific implementation, it is more useful to abstract their common capabilities: persistence, tool use, schedulability, portability, and observability. This article focuses on how self-evolution is implemented, with emphasis on the design of three layers: Prompt, Context, and Harness.

    2. What Self-Evolution Really Means: An Experience Loop, Not Weight Updates

    In an engineering context, self-evolution usually does not mean changes to training weights. It means the system changes its subsequent behavior through accumulated experience while running. It can be broken into four stages:

    1. Experience collection: record task inputs, tool calls, failure causes, and human corrections.
    2. Experience structuring: distill logs into reusable entries, such as “for this class of task, validate first and then write.”
    3. Experience injection: apply the experience in later tasks through context or prompt templates.
    4. Effect validation: use a regression task set to verify whether the new experience actually improves the success rate; otherwise, roll it back.

    Memory without the fourth stage only accumulates noise. The engineering essence of self-evolution is therefore a closed loop with an evaluation gate, not an endlessly appended history.

    3. Prompt Layer: Roles, Constraints, and Experience as Versioned Assets

    A prompt should not be a single long block of text; it should be managed in layers: the system layer defines identity and boundaries; the capability layer describes tool usage conventions; the experience layer carries filtered preferences and lessons; the task layer holds only the current goal. Experience entries should carry metadata to support retrieval and retirement:

    1. [Experience entry]
    2. id: exp-0142
    3. Applicable scenario: batch file writes
    4. Conclusion: do a small-sample trial write before the full run
    5. Evidence: 3 successes / 1 rollback
    6. Status: enabled

    Prompts must be placed under version control and canary release. Any experience injection is treated as a change that can be diffed and rolled back.

    4. Context Layer: Memory Governance Sets the Capability Ceiling

    Context is a limited budget and needs three-stage governance: retrieval, compression, and eviction. Short-term context holds the current task chain; mid-term context holds task summaries and decision points; long-term memory keeps vector retrieval and structured entries side by side—the former is good at semantic recall, the latter at precise constraints. Compression should keep conclusions and failure causes while discarding lengthy raw output. Eviction should be based on hit count and recency to prevent stale experience from interfering. In engineering terms, memory can be written to object storage, and retrieval results can be decoupled through a message queue to form a stable pipeline.

    5. Harness Layer: Tools, Scheduling, and Sandboxing Provide Action Capability

    The Harness is the execution framework between the agent and the external world. It includes tool registration with tiered permissions, scheduled execution, sandbox isolation, retries and idempotency, and audit logs. Long-running operation means handling long tasks and failure recovery: task state must be persistable, execution must be idempotent, and failures must be replayable. Illustrative pseudocode:

    1. def run_task(task):
    2. ctx = memory.recall(task.goal, k=8)
    3. plan = planner.compose(task, ctx, tools=registry.list(task.scope))
    4. for step in plan:
    5. result = sandbox.execute(step, timeout=step.budget)
    6. memory.write_summary(step, result)
    7. if not result.ok:
    8. return retry_or_escalate(step, result)
    9. return evaluate(task, regression_suite)

    Permissions should follow the least-privilege principle, and write operations and operations with external side effects require explicit confirmation.

    6. Migration, Observability, and Evaluation

    Portability requires separating state from configuration: memory, prompt templates, and tool definitions are all exportable assets. Observability should cover the call chain, tokens and cost, tool success rate, and memory hit rate. For evaluation, maintain a fixed regression set and online canary metrics; only a statistically significant improvement allows an experience entry to be solidified. If general cloud capabilities are used, a container platform can host the runtime, and logging services plus monitoring and alerting can close the loop. Only general categories are described here, without reference to specific vendor interfaces.

    7. Conclusion

    The competitiveness of long-running agents comes from the coordination of three layers: the Prompt layer provides versioned behavioral norms, the Context layer provides governed experience supply, and the Harness layer provides safe and recoverable action capability. Together with an evaluation gate, they form sustainable self-evolution. Baidu AI Cloud has accumulated general engineering practices in agent runtimes and observability systems, which can serve as a reference perspective for architecture design.