Architecture and Three Integration Patterns for AI Agent Execution Frameworks
AI models can generate decisions, but production software systems need an execution layer to enforce permissions, budgets, observability, and recovery. This article explains why an Agent Harness is necessary, compares CLI, SDK, and long-running service integration patterns, and outlines five core modules for building controllable and maintainable AI coding workflows.
1. The Starting Point: Model Capability Does Not Equal Production Readiness
The first attempt to integrate a large language model into software development is often a chat interface: a developer pastes code, and the model provides suggestions. But when the task changes from “explain this code” to “fix this failing test,” the interaction model must evolve.
The model needs to plan its own steps, call tools, read execution results, determine whether the task succeeded, and iterate when it fails. This runtime layer for orchestration, constraint enforcement, and activity tracking is commonly known as an agent execution framework, or Agent Harness.
The reason for introducing this layer is straightforward: the model generates decisions, while the engineering system must make those decisions controllable, reproducible, and reversible. Without it, an agent can quickly become unreliable in a real codebase. It may repeatedly modify the same file, consume its entire budget without producing useful output, execute unauthorized destructive commands, or leave no trace that can be investigated afterward.
2. Three Integration Patterns: Who Owns the Task Lifecycle?
Mature execution frameworks typically expose three types of interfaces. The fundamental difference is which component owns the task lifecycle.
| Integration pattern | Lifecycle owner | Long-running? | Typical use cases |
|---|---|---|---|
| CLI entry point | The process itself | No; exits after completion | CI pipelines, batch scripts, one-off local debugging |
| Programmatic API (SDK) | The calling application | Determined by the caller | Custom backends, internal developer platforms, event-driven automation |
| Long-running service | The service process | Yes | Multi-user systems, persistent sessions, and human approval workflows |
1. CLI Integration: A Short-Lived Entry Point Built for Automation
The value of a CLI entry point is zero-friction integration. It requires no service deployment or long-lived connection management, and its exit code can directly represent the result. This makes it a natural fit for batch code checks, automated unit tests, and scripted migration tasks.
# Example: run a constrained agent task in a CI pipelineagent run --task "Fix lint errors" \--budget 20000 \--allow read,patch \--deny network,git-push
The key design principle is to parameterize the budget and permission allowlist. This allows the same entry point to maintain consistent constraints in both local development and production pipelines.
2. Programmatic API: Returning Control to Application Code
When the agent needs to become one component of a business system rather than an opaque command, a programmatic API is the appropriate choice. It allows the calling application to explicitly control startup, pausing, and resumption while consuming intermediate progress through a stream of events.
session = agent.start(task="Add unit tests for the new API", budget=30000)for event in session.events():if event.type == "tool_call":audit.log(event) # Record the event for auditingelif event.type == "approval_required":event.approve(decision) # Request human interventionelif event.type == "completed":breaksession.pause() # Support interruption and resumption for cost control
Streaming events are the core of this integration pattern. They allow the surrounding system to govern the agent at the level of “what is the agent doing now?” rather than waiting for a final result.
3. Long-Running Service: A Core Component for Enterprise Integration
In the service model, the execution framework runs as an independent background process and communicates through a JSON-based RPC protocol. It typically provides four capabilities:
- Persistent sessions: Maintain context across requests.
- Event notifications: Proactively notify clients when the state changes.
- Human approval callbacks: Suspend high-risk operations until confirmation is received.
- External tool registration: Expose internal system capabilities to the agent.
The decision criterion is clear: when an application needs to use agent capabilities directly and should not depend on a client-side program, the service model is the appropriate option.
3. Five Core Internal Modules
Main Loop: Plan, Execute, Verify, and Repair
The main loop is not simply a while loop. It is a state machine with explicit termination conditions:
- Break the goal into steps.
- Select the appropriate tool.
- Verify the result.
- Correct the plan when the result fails.
In production systems, success criteria must be defined explicitly. Otherwise, the loop has no reliable way to converge.
Contract System: Turning Goals and Boundaries into Enforceable Constraints
The contract layer defines three elements:
- The task objective
- The token budget
- The set of permitted operations
Its purpose is to turn “avoid infinite loops” from a prompt-level instruction into a runtime constraint. When the budget is exhausted, execution stops. Unauthorized operations are rejected immediately.
Sandbox: Enforcing Least Privilege
File-system access, shell execution, and network access should be controlled separately. High-risk operations such as deletion, pushing changes, and deployment should require explicit human confirmation rather than relying on the model to behave safely on its own.
Context Compaction: A Cost Control Mechanism for Long Tasks
During long-running tasks, the history can expand rapidly. Common compaction strategies include:
- Preserving the original task objective and constraints
- Summarizing completed steps
- Removing redundant tool output
Compaction must not discard information required for the agent’s current decision.
End-to-End Auditing: The Foundation of Troubleshooting
All tool calls, file modifications, and approval records should be retained in full. Without auditing, it is impossible to identify the cause of an incorrect agent decision or build trust in the system across the engineering team.
4. Selection and Governance Recommendations
- Choose the integration pattern before choosing the model: The integration pattern determines governance costs; the model remains a replaceable component.
- Use least privilege by default: Prefer allowlists over denylists, and disable network access and write operations by default.
- Treat the budget as a safety valve: Every task should have limits on both tokens and execution steps.
- Keep humans in the loop: Place approval gates before irreversible operations.
- Prioritize observability: Logging, monitoring alerts, and metric dashboards should be ready before the pilot begins. Container platforms and logging services can provide the underlying infrastructure.
- Start with a small pilot: Begin with read-only analysis tasks and gradually enable write permissions.
5. Conclusion
The value of an execution framework is not that it makes an agent “smarter,” but that it makes the agent more controllable. The three integration patterns cover the full range from scripts to enterprise systems, while the five core modules establish the system’s safety boundaries. Once these two layers are designed properly, different model capabilities can be introduced while still producing stable, operable engineering outcomes.
