Engineering the Shell Around Coding Agents: A Four-Layer Design from Demo to Production

    This article explains why coding agents that work well in demos often fail in real engineering workflows. It describes a four-layer shell around the model: context management, tool orchestration with permission boundaries, verification loops with stop conditions, and session assets. It also provides a checklist for moving such tools toward multi-user production use.

    1. Why Demos Are Easy and Delivery Is Hard

    A demo usually involves one user, one code repository, and one complete conversation. If something fails, you simply start over. Real engineering is different. Tasks get interrupted, changes need to be rolled back, and the same requirement may be split into several branches explored in parallel. Team members also need to review every step the model took.

    An agent’s capability comes mainly from the model, but its reliability comes mainly from the shell around it. A model does not remember on its own which project conventions must not be broken. It also does not know which directories affect production configuration. The engineering system must provide this information explicitly and enforce its limits explicitly.

    Halfway through an AI-assisted programming competition, a participant’s coding tool suddenly stopped working, and most participants could not keep going for a while. This example shows that handing all the work to one external agent means depending on a component you cannot control. Engineering should address two things. First, when a tool fails, the workflow still needs a fallback path. Second, key artifacts should be stored in the repository and documentation, not only in the conversation.

    2. Layer 1: Context Management

    The context window is a limited resource. Placing an entire repository in the prompt is expensive, and it reduces the model’s attention quality. Context management needs to answer four questions.

    1. Which files enter the prompt? The usual priority order is: files the user explicitly names, source code directly related to the task, interface definitions and tests, and project convention documents. Build artifacts, dependency directories, and large data files should be excluded by default.
    2. Which history needs compression? In long conversations, early trial and error takes up a lot of space. The useful information is often just the final conclusions and open issues. When compressing, keep decisions, constraints, and to-do items, and discard the details of intermediate failed attempts.
    3. When should a summary be generated? Common triggers include completing a subtask, context usage exceeding a set share of the budget, and switching to a new module. Write summaries to a file, not only to memory, so that work can resume after an interruption.
    4. When should source code be re-read? This is the point most often overlooked. A model that edits code based on a summary or old memory can easily drift from the actual file contents. Re-reading the target file before every edit is a basic discipline that keeps an agent from making the code worse over time.

    Here is a simplified example that illustrates the approach:

    1. def build_context(task, repo, budget_tokens):
    2. context = [task.spec] # Always keep the task specification
    3. context += repo.files_related_to(task) # Sorted by relevance
    4. context += repo.conventions_docs() # Project conventions
    5. return trim_to_budget(context, budget_tokens)
    6. def before_edit(path, cached_snapshot):
    7. current = read_file(path) # Re-read the file before editing
    8. if current != cached_snapshot:
    9. raise StaleContextError(path) # Content has changed, refresh context first
    10. return current

    The check in before_edit is simple, but it prevents a common problem. A model generates a patch from an outdated version and overwrites changes someone else has just committed.

    3. Layer 2: Tool Orchestration and Permission Boundaries

    Agents commonly use tools in a few categories: reading files, modifying files, running commands, calling external services, and requesting user authorization. More tools are not always better. Each added tool widens the model’s choices, raises the chance of misuse, and makes permission auditing harder.

    Three principles should guide the design.

    • Least privilege. Read operations can be allowed by default. Modifications should be limited to task-related directories. Command execution should use an allowlist.
    • Dangerous operations require confirmation. Deleting, overwriting, publishing, changing permission settings, and sending data to external parties should pause and request explicit user approval.
    • Boundaries must be describable and auditable. Every tool call should record its parameters, its result, and the context that triggered it, so that it can be traced later.

    To check whether a permission design is adequate, ask one question: if the agent gets a given tool, is the worst possible outcome acceptable? A clear yes means the design is sound. An agent with broad, poorly defined permissions is more dangerous than one with fewer tools and clear boundaries.

    Connecting external tools also requires a unified interface protocol and authentication method. Credentials should be stored in environment variables or a dedicated secrets management service. They should not be written into prompts or appear in logs.

    4. Layer 3: Verification Loop

    An agent’s output counts as work product only after it has been verified. The basic verification loop is as follows: modify the code, run unit tests, lint checks, and type checks, collect the errors, feed them back to the model as new input, and then decide whether to continue fixing or stop.

    The key is the stop condition. A loop without one lets the agent keep circling the same error. It consumes large amounts of compute and may keep changing unrelated code while failing.

    1. MAX_ATTEMPTS = 3
    2. def verify_and_fix(task):
    3. seen_errors = set()
    4. for attempt in range(MAX_ATTEMPTS):
    5. result = run_checks() # Tests, lint, and type checks
    6. if result.passed:
    7. return "done"
    8. signature = result.error_signature() # Normalize errors so they can be compared
    9. if signature in seen_errors:
    10. return "ask_human" # Same error repeats, hand off to a person
    11. seen_errors.add(signature)
    12. apply_fix(task, result.errors) # Feed the errors back to the model
    13. return "ask_human" # Attempt limit reached, stop and explain

    This structure has three elements: an attempt limit, repeated-error detection, and a clear handoff point to a human. When it stops, the agent should state what it has completed, why it failed, and which decisions require a person. A vague message such as “unable to complete” is not enough.

    5. Layer 4: Session Assets

    Session management may look like a convenience feature, but it determines whether a tool can be used over the long term. Real engineering consists of a series of interruptions, rollbacks, forks, and reviews. A single conversation cannot solve these problems. What is needed is a work state that can be picked up again.

    Session assets usually include the following capabilities:

    • Session records: Keep the full prompts, model outputs, tool calls, and results, and support replay.
    • Resume: After an interruption, continue from the most recent checkpoint instead of starting over.
    • Forking: Try two approaches from the same starting point without interference, then choose to merge or discard either one.
    • Rollback: File changes should be reversible to any checkpoint. The scope of each rollback should be clear, and it must not undo the user’s own changes.
    • Diagnostics: When an agent behaves abnormally, inspect its configuration, context usage, permission status, and recent tool errors.

    These capabilities depend on externalized state. State should be stored in files, databases, or a version control system, not only in the model’s temporary memory.

    6. Checklist for Moving from Prototype to Production

    When a coding agent tool is opened to multiple users, check the following items:

    1. Does the context have clear selection rules, a budget limit, and a compression strategy?
    2. Does the agent re-read files before editing, and does it detect whether their contents have changed?
    3. Does each tool have a clearly defined permission scope, and do dangerous operations require confirmation?
    4. Does the verification process cover tests, coding standards, and type checks, and does it define stop conditions?
    5. Can sessions be saved, resumed, forked, and rolled back?
    6. Are credentials isolated from prompts, logs, and code?
    7. If the tool becomes unavailable, does the team have an alternative workflow?