Three Pillars of AI Agent Architecture: Prompt, Context, and Harness Engineering

    A practical look at the three layers that shape reliable AI agent behavior: static and dynamic prompt construction, context management within limited windows, and harness-level execution controls. The article explains what each layer does, common failure modes, acceptance metrics, and a recommended implementation order, without assigning universal scores to any layer.

    In real-world development, many teams find that calling a general-purpose large model API directly for complex programming tasks often produces unstable results. The same base model can perform noticeably better when run inside a mature command-line AI coding tool. The difference is often not the model itself but the engineering around it.

    The industry commonly divides this surrounding design into three layers: prompt engineering, context engineering, and harness engineering. They answer three questions: how to say it, what the model should see, and what environment it runs in. The three layers build on one another and complement each other. This article explains the practices, common failures, and acceptance methods for each layer. Note that this article does not assign a single score to each layer. Results depend on task type, model version, and evaluation method; numbers without a measurement basis have little reference value.

    1. Prompt Engineering: Static Rules and Dynamic Information Assembly

    The prompt is the first interface between an agent and the model. A reliable system prompt usually contains two parts.

    The static part defines the role, goals, and boundaries, including coding standards, prohibited operations (for example, deleting system directories), and output format (for example, requiring compliance with a specific JSON Schema). Keeping the static part stable helps reduce outputs that deviate from expectations.

    The dynamic part collects the runtime environment at startup, including operating system type, current working directory, dependency versions, and current branch. This information lets the model understand its environment when handling the first task.

    Example (pseudocode, structure only):

    1. {
    2. "system_prompt": "You are a senior development assistant currently running in {os_type}.",
    3. "dynamic_context": {
    4. "cwd": "/project/src",
    5. "git_branch": "feature/agent-impl",
    6. "recent_files": ["main.py", "utils.py"]
    7. }
    8. }

    Prerequisites: the collection script needs permission to read directories and version information; the cwd field should be an absolute path; recent_files must be limited in number to avoid stuffing an entire directory listing into the prompt.

    There are two common failure modes. First, environment information is hardcoded in the prompt and stops working on another machine. Second, the static rules are too long, and key constraints get buried.

    2. Context Engineering: Managing Information in a Limited Window

    If prompt engineering addresses “how to say it,” context engineering addresses “what the model sees.” Long-running tasks continuously produce tool returns, error logs, and conversation history, and the window quickly fills up. The goal of context engineering is to make the information used for each decision both complete and relevant.

    1. Compress history

    • Rewrite successfully completed operations as a one-sentence structured summary. For example: “Read utils.py; contains functions parse_config and load_env.”
    • For very long error stacks, keep only the exception type, trigger location, and the last business-relevant call.
    • Store large blocks of tool-returned text in external storage, keeping only the path and summary in context.

    2. Retention strategy

    • Always retain global instructions, the current subtask goal, and unresolved errors.
    • Retain full records of the most recent N steps. N must be determined through testing on real tasks; a common starting point is 5 to 10 steps, after which records are converted to summaries.
    • Downgrade early exploratory records to summaries or move them out of the main context.

    3. Isolation rules

    • Subtasks use independent contexts and write only conclusions back to the main context when finished.
    • File content unrelated to the current subtask does not enter the context.
    • Label content by source (user instruction, tool output, file content) so the model can distinguish them.

    4. Attention priority

    Order information as follows: global constraints, current goal, recent errors, recent N steps, early summaries. When the window is insufficient, trim from the last item.

    Context assembly template

    1. [Global constraints]
    2. {system_rules}
    3. [Current goal]
    4. {current_subtask}
    5. [Open issues]
    6. {open_issues}
    7. [Recent operations, most recent N steps]
    8. {recent_steps}
    9. [Historical summaries]
    10. {compressed_history}
    11. [Available tools]
    12. {tool_list}

    Acceptance metrics

    • Task completion rate: the proportion of tasks ultimately completed within the same batch.
    • Irrelevant information ratio: manual sampling to check the proportion of context content unrelated to the current goal.
    • Tokens per step: compare with an uncompressed version and observe whether the growth curve levels off.
    • Repeated operations count: whether the model repeatedly reads the same file or executes the same command.

    3. Harness Engineering: Building an Execution Scaffold for the Model

    Harness engineering focuses on “what environment it runs in.” Model outputs are uncertain, and the harness layer’s job is to constrain that uncertainty with external structure. It is an important means of improving reliability, but not every scenario requires it. For one-off simple Q&A, the extra cost may outweigh the benefit.

    1. Control loop

    An agent usually advances tasks through a “think, act, observe” loop. The harness layer handles three things:

    • Set a maximum iteration count to prevent infinite loops.
    • Detect output format anomalies, reset the format, and retry when anomalies occur.
    • Use a state machine to decompose tasks, defining allowed transitions for each state to reduce the room for free-form model behavior.

    2. Tool execution and fault tolerance

    When the model wants to execute a shell command or modify a file, the harness layer should not forward it directly. It should:

    • Execute in a sandbox and restrict accessible directories.
    • Follow the principle of least privilege, exposing only the tools and paths required by the task.
    • Use a command allowlist and reject commands not on the list.
    • Set timeouts and capture standard output and standard error.
    • Redact sensitive data (keys, tokens, personal information) before it enters context; logs should also not retain plaintext.
    • Automatically back up files before modification so they can be rolled back on failure.
    • On failure, return a structured error (error code, command, key error line) to the model so it can correct itself. Neither terminate the task immediately nor put the full stack back into context unchanged.

    3. External resource access

    In complex scenarios, an agent may need to read internal API documentation, coding standards, or runtime metrics. A common approach is to connect through standard interfaces for object storage, vector databases, or logging systems. Access should respect data access permissions, read only the data needed for the current task, and log each access.

    Three-layer comparison

    Dimension Prompt Engineering Context Engineering Harness Engineering
    Core question How to say it What the model sees What environment it runs in
    Main inputs Role, rules, environment information History, tool output, file content Loop state, tool calls, permission configuration
    Main outputs System prompt structure Context assembled at each step Constrained execution results and error feedback
    Typical failures Conflicting rules, outdated environment information Window overflow, irrelevant information interference Infinite loops, unauthorized operations, unrecoverable errors
    Acceptance metrics Format compliance rate, rule adherence rate Task completion rate, token usage trend Iteration count distribution, unauthorized action blocks, rollback success rate

    4. Implementation Order

    1. First write the static prompt and environment collection to ensure every run starts from a consistent baseline.
    2. Then add context compression and retention strategies, and measure completion rate on real long tasks.
    3. Finally add harness-layer loop control and tool sandboxing. Start by exposing only read-only tools, then gradually enable write permissions.

    After completing each layer, compare the same batch of tasks before and after the change, and proceed to the next layer only after confirming effectiveness.

    Conclusion

    Each of the three engineering layers has its own role. Prompt engineering defines the communication method, context engineering controls the amount and order of information entering the model, and harness engineering limits what the model can do and how errors are handled. If any layer is missing, the system will expose problems somewhere in the chain. Starting with a small scope of tasks and validating each layer with measurable metrics is more reliable than pursuing a high score all at once.