TypeScript Agent Loop Human Approval Gates
In this article
- Why Human Approval Gates Matter
- Core Requirements of an Approval Gate
- How Approval Gates Work in a TypeScript Agent Loop
- Types of Actions That Need Approval
- Designing the Approval Prompt
- Common Mistakes and Pitfalls
- Comparing Approval Gates to Other Safety Mechanisms
- Costs and Trade-offs
- Frequently Asked Questions
This article explains what human approval gates in a TypeScript agent loop must do: they must pause an autonomous AI agent at a defined point, present a clear summary of the pending action, request explicit user confirmation, and only then allow the action to proceed. These gates are the primary safety mechanism for preventing costly or irreversible mistakes in AI-driven workflows.
Why Human Approval Gates Matter
Agent loops are software systems that autonomously decide and execute a sequence of steps to achieve a goal. Without human oversight, an agent could delete files, send emails, modify databases, or spend money based on a misinterpreted instruction. Approval gates are checkpoints where the loop stops and waits for a person to review what the agent is about to do. In TypeScript, these gates are typically implemented as async functions that return a promise, which resolves only when the user has made a decision. The gate is not just a technical pause; it is a control point that defines the boundary between autonomous action and human responsibility.
Core Requirements of an Approval Gate
A well-designed approval gate must fulfill several functions simultaneously. It must be explicit about what action is pending, provide enough context for a human to make an informed decision, and record the outcome for auditability. The gate should also handle timeouts and failures gracefully, so the agent does not hang indefinitely if the user is away.
- Action summary: A concise description of what the agent intends to do, including the target resource (e.g., file path, email recipient, API endpoint).
- Impact assessment: What will change, what is irreversible, and what the cost or risk is if the action is taken.
- Alternatives: If the user rejects the action, the gate should allow the agent to propose a modified plan or ask for clarification.
- Audit trail: The gate must log the action, the user's decision, and the timestamp for later review.
- Timeout handling: If no response is received within a set time, the gate should either cancel the action or escalate to a fallback approver.
How Approval Gates Work in a TypeScript Agent Loop
In a typical TypeScript agent loop, the agent runs a cycle of perceiving, deciding, and acting. The approval gate is inserted between the decision and the action. Here is a simplified pseudocode structure:
async function runAgentLoop() {
while (true) {
const action = await agent.decideNextAction();
if (action.requiresApproval) {
const approved = await requestApproval(action);
if (!approved) {
await agent.handleRejection(action);
continue;
}
}
await agent.execute(action);
}
}
The requestApproval function is where the gate logic lives. It must be implemented as an asynchronous function that blocks the loop until the user responds. In practice, this could be a CLI prompt, a web-based approval UI, or a message to a Slack channel. The key is that the agent cannot proceed until the promise resolves with a definitive yes or no.
Types of Actions That Need Approval
Not every action in an agent loop requires human approval. The goal is to balance autonomy with safety. The following categories typically require a gate:
- Destructive operations: Deleting files, dropping database tables, or removing cloud resources.
- External communications: Sending emails, posting to social media, or publishing content.
- Financial transactions: Purchasing items, transferring money, or changing billing settings.
- Security-sensitive changes: Modifying permissions, adding users, or changing firewall rules.
- Irreversible actions: Any operation that cannot be undone, such as overwriting a backup or terminating a production server.
Designing the Approval Prompt
The prompt presented to the human is the most critical part of the gate. A vague prompt like "Approve action?" is useless because the user cannot make an informed decision. The prompt should include:
- What: The exact operation to be performed (e.g., "Delete file /tmp/data.csv").
- Why: The reason the agent chose this action (e.g., "User requested cleanup of temporary files").
- Consequences: What will happen if the action is taken (e.g., "The file will be permanently removed and cannot be recovered").
- Context: Any relevant recent steps or data that led to this decision.
In a TypeScript implementation, you might define an interface for the approval request:
interface ApprovalRequest {
id: string;
action: string;
reason: string;
impact: string;
createdAt: Date;
}
This structure ensures that all necessary information is passed to the UI layer.
Common Mistakes and Pitfalls
Implementing approval gates is straightforward, but several common mistakes can undermine their effectiveness. One mistake is making the gate too permissive, such as allowing the agent to retry a rejected action multiple times without additional human input. Another is failing to include enough context in the prompt, leading to uninformed approvals. A third is not handling timeouts, which can cause the agent to stall indefinitely or, worse, auto-approve after a timeout without explicit consent. Finally, some implementations log approvals but not the full context, making audits difficult.
Comparing Approval Gates to Other Safety Mechanisms
Approval gates are not the only way to control an agent. Other mechanisms include:
- Sandboxing: Running the agent in a restricted environment where it cannot access sensitive resources, regardless of approvals.
- Rate limiting: Restricting the number of actions per minute to limit damage.
- Read-only mode: Allowing the agent to read data but not modify anything.
- Human-in-the-loop for all actions: A stricter approach where every action requires approval, which is safer but less autonomous.
Approval gates are best used in combination with sandboxing and read-only modes. They are not a substitute for technical restrictions.
Costs and Trade-offs
Adding approval gates introduces latency and requires human attention. If the gate is too frequent, the agent becomes tedious to use. If it is too rare, the risk of mistakes increases. The cost is not just time but also cognitive load on the user, who must review each request. In a production setting, you might implement a policy engine that decides which actions need approval based on risk level, and you should allow users to set their own thresholds.
Frequently Asked Questions
Can an approval gate be bypassed by the agent?
No, if the gate is implemented correctly as a hard pause in the loop. The agent's code cannot proceed past the await until the promise resolves. However, if the agent has access to the same function that approves actions, it could theoretically call it itself. To prevent this, the approval function must be isolated from the agent's code, for example, by running it in a separate process or requiring a cryptographic signature that the agent does not possess.
What happens if the user rejects an action?
The agent should receive a rejection signal and then decide on an alternative course of action. It could ask for clarification, modify the action, or skip it entirely. The rejection should be logged, and the agent should not repeat the same action without new information.
How long should an approval gate wait for a response?
There is no universal answer. It depends on the context. For a batch job, you might wait hours; for an interactive session, you might wait only a few minutes. The gate should have a configurable timeout, and on timeout, the default behavior should be to cancel the action, not to auto-approve. Auto-approval on timeout defeats the purpose of the gate.
Should every agent action require approval?
No, that would eliminate the benefit of autonomy. The goal is to require approval only for actions that are high-risk, irreversible, or external. You can define a risk score for each action and set a threshold above which approval is required.
Can approval gates be used in a CI/CD pipeline?
Yes, they are common in deployment pipelines where a bot proposes changes and a human must approve before the deployment proceeds. In TypeScript, you can integrate the gate with the pipeline's API, such as pausing a job until a webhook receives a confirmation.
How do I test an approval gate?
You can write unit tests that mock the approval function to return true or false and verify that the agent proceeds or stops accordingly. You should also test the timeout behavior and the rejection flow. Integration tests can simulate a real user interaction with the prompt UI.