|
| 1 | +--- |
| 2 | +title: "Human-in-the-Loop" |
| 3 | +pageOrder: 7 |
| 4 | +description: "Build a Kubernetes-native AI agent that pauses and asks for your approval before taking destructive actions." |
| 5 | +--- |
| 6 | + |
| 7 | +export const metadata = { |
| 8 | + title: "Human-in-the-Loop with kagent", |
| 9 | + description: "Build a Kubernetes-native AI agent that pauses and asks for your approval before taking destructive actions.", |
| 10 | + author: "kagent.dev" |
| 11 | +}; |
| 12 | + |
| 13 | +# Human-in-the-Loop with kagent |
| 14 | + |
| 15 | +AI agents that can take action are powerful — but you don't always want them acting without your say-so. This tutorial walks you through building a Kubernetes-native AI agent that **pauses and asks for your approval** before doing anything destructive. |
| 16 | + |
| 17 | +## What You'll Build |
| 18 | + |
| 19 | +By the end of this tutorial, you'll have an agent running on a local Kind cluster that: |
| 20 | + |
| 21 | +- **Reads cluster resources** freely (no approval needed) |
| 22 | +- **Pauses for your approval** before creating, modifying, or deleting resources |
| 23 | +- **Asks you questions** when it needs more information |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +Make sure you have these installed before starting: |
| 30 | + |
| 31 | +| Tool | Install Link | |
| 32 | +|------|-------------| |
| 33 | +| Docker | [get-docker](https://docs.docker.com/get-docker/) | |
| 34 | +| Kind | [quick-start](https://kind.sigs.k8s.io/docs/user/quick-start/#installation) | |
| 35 | +| kubectl | [install-tools](https://kubernetes.io/docs/tasks/tools/) | |
| 36 | +| Helm | [install](https://helm.sh/docs/intro/install/) | |
| 37 | + |
| 38 | +You'll also need an **OpenAI API key**. |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## Step 1 — Create a Kind Cluster |
| 43 | + |
| 44 | +Spin up a local Kubernetes cluster: |
| 45 | + |
| 46 | +```bash |
| 47 | +kind create cluster --name kagent-hitl |
| 48 | +``` |
| 49 | + |
| 50 | +Verify it's running: |
| 51 | + |
| 52 | +```bash |
| 53 | +kubectl cluster-info --context kind-kagent-hitl |
| 54 | +``` |
| 55 | + |
| 56 | +You should see the control plane address printed. If so, you're good to go. |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## Step 2 — Install kagent |
| 61 | + |
| 62 | +There are two Helm charts to install: the CRDs first, then kagent itself. |
| 63 | + |
| 64 | +**Install the CRDs:** |
| 65 | + |
| 66 | +```bash |
| 67 | +helm install kagent-crds oci://ghcr.io/kagent-dev/kagent/helm/kagent-crds \ |
| 68 | + --namespace kagent \ |
| 69 | + --create-namespace |
| 70 | +``` |
| 71 | + |
| 72 | +**Set your API key:** |
| 73 | + |
| 74 | +```bash |
| 75 | +export OPENAI_API_KEY="your-api-key-here" |
| 76 | +``` |
| 77 | + |
| 78 | +**Install kagent:** |
| 79 | + |
| 80 | +```bash |
| 81 | +helm install kagent oci://ghcr.io/kagent-dev/kagent/helm/kagent \ |
| 82 | + --namespace kagent \ |
| 83 | + --set providers.default=openAI \ |
| 84 | + --set providers.openAI.apiKey=$OPENAI_API_KEY |
| 85 | +``` |
| 86 | + |
| 87 | +**Wait for everything to come up:** |
| 88 | + |
| 89 | +```bash |
| 90 | +kubectl wait --for=condition=ready pod --all -n kagent --timeout=120s |
| 91 | +``` |
| 92 | + |
| 93 | +Once all pods report `condition met`, move on. |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## Step 3 — Deploy the Agent |
| 98 | + |
| 99 | +This is the core of the tutorial. You'll create an agent that uses kagent's **built-in Kubernetes tools** (served via the kagent tool server), with **approval gates** on the destructive ones. |
| 100 | + |
| 101 | +Save this as `hitl-agent.yaml`: |
| 102 | + |
| 103 | +```yaml |
| 104 | +apiVersion: kagent.dev/v1alpha2 |
| 105 | +kind: Agent |
| 106 | +metadata: |
| 107 | + name: hitl-agent |
| 108 | + namespace: kagent |
| 109 | +spec: |
| 110 | + description: A Kubernetes agent with human-in-the-loop approval for destructive operations. |
| 111 | + type: Declarative |
| 112 | + declarative: |
| 113 | + modelConfig: default-model-config |
| 114 | + systemMessage: | |
| 115 | + You are a Kubernetes management agent. You help users inspect and manage |
| 116 | + resources in the cluster. Before making any changes, explain what you |
| 117 | + plan to do. If the user's request is ambiguous, use the ask_user tool |
| 118 | + to clarify before proceeding. |
| 119 | + tools: |
| 120 | + - type: McpServer |
| 121 | + mcpServer: |
| 122 | + name: kagent-tool-server |
| 123 | + kind: RemoteMCPServer |
| 124 | + apiGroup: kagent.dev |
| 125 | + toolNames: |
| 126 | + - k8s_get_resources |
| 127 | + - k8s_describe_resource |
| 128 | + - k8s_get_pod_logs |
| 129 | + - k8s_get_events |
| 130 | + - k8s_get_resource_yaml |
| 131 | + - k8s_apply_manifest |
| 132 | + - k8s_delete_resource |
| 133 | + - k8s_patch_resource |
| 134 | + requireApproval: |
| 135 | + - k8s_apply_manifest |
| 136 | + - k8s_delete_resource |
| 137 | + - k8s_patch_resource |
| 138 | +``` |
| 139 | +
|
| 140 | +Create the agent: |
| 141 | +
|
| 142 | +```bash |
| 143 | +kubectl create -f hitl-agent.yaml |
| 144 | +``` |
| 145 | + |
| 146 | +**Here's what each tool does:** |
| 147 | + |
| 148 | +| Tool | What Happens | |
| 149 | +|------|-------------| |
| 150 | +| `k8s_get_resources` | Runs immediately — lists pods, services, deployments, etc. | |
| 151 | +| `k8s_describe_resource` | Runs immediately — shows resource details | |
| 152 | +| `k8s_get_pod_logs` | Runs immediately — reads pod logs | |
| 153 | +| `k8s_get_events` | Runs immediately — shows cluster events | |
| 154 | +| `k8s_get_resource_yaml` | Runs immediately — exports resource YAML | |
| 155 | +| `k8s_apply_manifest` | **Pauses for approval** — creates or updates resources | |
| 156 | +| `k8s_delete_resource` | **Pauses for approval** — deletes resources | |
| 157 | +| `k8s_patch_resource` | **Pauses for approval** — modifies resources | |
| 158 | +| `ask_user` | Built-in on every agent — asks you questions anytime | |
| 159 | + |
| 160 | +The key is `requireApproval` — any tool listed there will pause execution until you explicitly approve it. Read-only tools run freely; write operations need your sign-off. |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +## Step 4 — Open the UI |
| 165 | + |
| 166 | +Port-forward the kagent dashboard: |
| 167 | + |
| 168 | +```bash |
| 169 | +kubectl port-forward -n kagent svc/kagent-ui 8080:8080 |
| 170 | +``` |
| 171 | + |
| 172 | +Open [http://localhost:8080](http://localhost:8080) in your browser. You should see the kagent UI with your **hitl-agent** listed. |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +## Step 5 — Try It Out |
| 177 | + |
| 178 | +Now for the fun part. Run through these tests to see human-in-the-loop in action. |
| 179 | + |
| 180 | +### Test 1: Read Without Approval |
| 181 | + |
| 182 | +1. Select the **hitl-agent** in the UI |
| 183 | +2. Type: `List all pods in the kagent namespace` |
| 184 | +3. The agent calls `k8s_get_resources` — it runs **immediately** with no approval prompt |
| 185 | +4. You see the pod listing right away |
| 186 | + |
| 187 | +This shows that read-only tools are not gated. |
| 188 | + |
| 189 | +### Test 2: Approve a Create |
| 190 | + |
| 191 | +1. Type: `Create a ConfigMap called test-config in the default namespace with the key message set to "hello from kagent"` |
| 192 | +2. The agent calls `k8s_apply_manifest` — execution **pauses** |
| 193 | +3. You'll see **Approve / Reject** buttons appear, along with the YAML it wants to apply |
| 194 | +4. Click **Approve** |
| 195 | +5. The agent creates the ConfigMap and confirms |
| 196 | + |
| 197 | +### Test 3: Reject a Delete |
| 198 | + |
| 199 | +1. Type: `Delete the ConfigMap test-config in the default namespace` |
| 200 | +2. The agent calls `k8s_delete_resource` — execution **pauses** |
| 201 | +3. Click **Reject** and enter a reason: `I want to keep this ConfigMap for now` |
| 202 | +4. The agent sees your reason and responds accordingly — it doesn't delete anything |
| 203 | + |
| 204 | +### Test 4: Agent Asks You a Question |
| 205 | + |
| 206 | +1. Type: `Set up a namespace for my application` |
| 207 | +2. The request is vague, so the agent calls `ask_user` to clarify (e.g., "What should the namespace be called?") |
| 208 | +3. Answer the question and the agent continues with your input |
| 209 | + |
| 210 | +--- |
| 211 | + |
| 212 | +## How It Works |
| 213 | + |
| 214 | +The flow is straightforward: |
| 215 | + |
| 216 | +``` |
| 217 | +You send a message |
| 218 | + -> Agent decides which tool to call |
| 219 | + -> Is the tool in requireApproval? |
| 220 | + -> YES: Execution pauses, you see Approve/Reject in the UI |
| 221 | + -> Approve: tool runs normally |
| 222 | + -> Reject: agent receives your reason and adapts |
| 223 | + -> NO: Tool runs immediately |
| 224 | +``` |
| 225 | + |
| 226 | +The `ask_user` tool works the same way — the agent pauses, you answer, and it continues. Both use the same underlying confirmation mechanism, which keeps things simple. |
| 227 | + |
| 228 | +--- |
| 229 | + |
| 230 | +## Cleanup |
| 231 | + |
| 232 | +Delete the cluster when you're done: |
| 233 | + |
| 234 | +```bash |
| 235 | +kind delete cluster --name kagent-hitl |
| 236 | +``` |
| 237 | + |
| 238 | +--- |
| 239 | + |
| 240 | +## Key Takeaways |
| 241 | + |
| 242 | +- **`requireApproval`** is all you need — list the tools that need human sign-off |
| 243 | +- **Read-only tools run freely**, write operations pause for approval |
| 244 | +- **`ask_user`** is built-in on every agent — no extra config required |
| 245 | +- **Rejection reasons** are sent back to the LLM so it can adjust its approach |
0 commit comments