|
| 1 | +--- |
| 2 | +title: Use ai-devkit agent send --wait for Programmatic Agent Calls |
| 3 | +description: Use ai-devkit agent send --wait to call an existing interactive Claude Code, Codex, Gemini CLI, or OpenCode session from scripts and automation. |
| 4 | +order: 11 |
| 5 | +--- |
| 6 | + |
| 7 | +`ai-devkit agent send --wait` lets a script send a prompt to an already-running interactive coding agent, wait for the next assistant response, and print that response to stdout. |
| 8 | + |
| 9 | +This is useful when you want the ergonomics of an interactive agent session but need a programmatic interface similar to `claude -p` or `codex exec`. |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +Before using this workflow: |
| 14 | + |
| 15 | +- Install AI DevKit and make sure `ai-devkit` is available in your shell. |
| 16 | +- Start Claude Code, Codex, Gemini CLI, or OpenCode in the project you want to control. |
| 17 | +- Keep that agent running in a supported terminal session such as tmux, iTerm2, or Apple Terminal. |
| 18 | +- Run `ai-devkit agent list` and confirm the session appears. |
| 19 | + |
| 20 | +## Quick Example |
| 21 | + |
| 22 | +Start your coding agent normally in one terminal: |
| 23 | + |
| 24 | +```bash |
| 25 | +claude |
| 26 | +# or codex |
| 27 | +# or gemini |
| 28 | +# or opencode |
| 29 | +``` |
| 30 | + |
| 31 | +In another terminal, find the session identifier: |
| 32 | + |
| 33 | +```bash |
| 34 | +ai-devkit agent list |
| 35 | +``` |
| 36 | + |
| 37 | +Look for the `Agent` column. Use that value, or a unique partial match, as `--id`. |
| 38 | + |
| 39 | +```text |
| 40 | +Agent CWD Type Status |
| 41 | +my-project ~/code/my-project Claude Code wait |
| 42 | +``` |
| 43 | + |
| 44 | +Then send a prompt and wait for the response: |
| 45 | + |
| 46 | +```bash |
| 47 | +ai-devkit agent send "Summarize the current git diff and identify risks." --id my-project --wait |
| 48 | +``` |
| 49 | + |
| 50 | +The assistant response is printed to stdout. Status messages and warnings are written to stderr, so scripts can pipe or capture the response cleanly. |
| 51 | + |
| 52 | +## Why use this instead of claude -p or codex exec? |
| 53 | + |
| 54 | +Use `agent send --wait` when you want to talk to an existing interactive session instead of starting a fresh one-off process. |
| 55 | + |
| 56 | +That means the agent keeps its current: |
| 57 | + |
| 58 | +- conversation context |
| 59 | +- working directory |
| 60 | +- loaded project state |
| 61 | +- configured tools and MCP servers |
| 62 | +- authentication and permission state |
| 63 | +- terminal session and interactive approvals |
| 64 | + |
| 65 | +This makes it a strong fit for automation that should continue work inside the same Claude Code, Codex, Gemini CLI, or OpenCode session. |
| 66 | + |
| 67 | +## When should I still use claude -p or codex exec? |
| 68 | + |
| 69 | +Use a native non-interactive command when you want a brand-new isolated run, predictable startup behavior, and no dependency on a live terminal session. |
| 70 | + |
| 71 | +Use `ai-devkit agent send --wait` when the existing interactive session is the feature: you want to preserve context, reuse approvals, and keep the session visible for a human to inspect. |
| 72 | + |
| 73 | +| Use case | Prefer | |
| 74 | +|---|---| |
| 75 | +| Continue an existing interactive session with its current context | `ai-devkit agent send --wait` | |
| 76 | +| Run a fresh one-off prompt with no dependency on a live terminal | `claude -p` or `codex exec` | |
| 77 | +| Script against stdout while preserving agent session context | `ai-devkit agent send --wait` | |
| 78 | +| CI job that must not depend on a human terminal session | Native non-interactive command | |
| 79 | + |
| 80 | +## Which agents are supported? |
| 81 | + |
| 82 | +`agent send --wait` uses the same experimental agent detection system as `ai-devkit agent list`. |
| 83 | + |
| 84 | +Current interactive adapters include: |
| 85 | + |
| 86 | +- Claude Code |
| 87 | +- Codex |
| 88 | +- Gemini CLI |
| 89 | +- OpenCode |
| 90 | + |
| 91 | +Support depends on AI DevKit being able to detect the live process, find its terminal, and read its session transcript. |
| 92 | + |
| 93 | +Run this to confirm what AI DevKit can see on your machine: |
| 94 | + |
| 95 | +```bash |
| 96 | +ai-devkit agent list |
| 97 | +``` |
| 98 | + |
| 99 | +## How do I select the target session? |
| 100 | + |
| 101 | +Use `--id` with the agent name or a unique partial match from `agent list`. |
| 102 | + |
| 103 | +```bash |
| 104 | +ai-devkit agent send "What are you working on?" --id frontend --wait |
| 105 | +``` |
| 106 | + |
| 107 | +If multiple agents match the same identifier, AI DevKit asks you to use a more specific value. |
| 108 | + |
| 109 | +For scripts, prefer exact or highly specific identifiers. |
| 110 | + |
| 111 | +## Can I pipe a prompt from stdin? |
| 112 | + |
| 113 | +Yes. Use `--stdin`, or pipe input without a message argument. |
| 114 | + |
| 115 | +```bash |
| 116 | +git diff -- src/ tests/ | ai-devkit agent send --id my-project --stdin --wait |
| 117 | +``` |
| 118 | + |
| 119 | +You can also compose a prompt around command output: |
| 120 | + |
| 121 | +```bash |
| 122 | +{ |
| 123 | + printf 'Review this diff for bugs and missing tests:\n\n' |
| 124 | + git diff |
| 125 | +} | ai-devkit agent send --id my-project --stdin --wait |
| 126 | +``` |
| 127 | + |
| 128 | +## Can I get JSON output? |
| 129 | + |
| 130 | +Yes. Add `--json` with `--wait`. |
| 131 | + |
| 132 | +```bash |
| 133 | +ai-devkit agent send "Return a short implementation plan." --id my-project --wait --json |
| 134 | +``` |
| 135 | + |
| 136 | +The JSON output includes the target metadata, prompt, captured response messages, elapsed time, and final agent status. |
| 137 | + |
| 138 | +This is useful when your script needs more than plain text: |
| 139 | + |
| 140 | +```bash |
| 141 | +response_json="$(ai-devkit agent send "List the next 3 tasks." --id my-project --wait --json)" |
| 142 | +printf '%s\n' "$response_json" |
| 143 | +``` |
| 144 | + |
| 145 | +## How long does --wait wait? |
| 146 | + |
| 147 | +By default, AI DevKit waits up to 10 minutes. |
| 148 | + |
| 149 | +Use `--timeout` to set a custom limit in milliseconds: |
| 150 | + |
| 151 | +```bash |
| 152 | +ai-devkit agent send "Run the focused tests and report failures." --id my-project --wait --timeout 30000 |
| 153 | +``` |
| 154 | + |
| 155 | +`--timeout` only works together with `--wait`. |
| 156 | + |
| 157 | +## What exactly is printed? |
| 158 | + |
| 159 | +In plain text mode, AI DevKit prints new assistant messages created after the prompt was sent. |
| 160 | + |
| 161 | +It does not print older conversation history, and it does not print the user prompt back to stdout. |
| 162 | + |
| 163 | +Warnings, status messages, and timeout errors are sent to stderr. This keeps stdout suitable for command substitution and pipelines. |
| 164 | + |
| 165 | +## Does this execute commands directly? |
| 166 | + |
| 167 | +No. AI DevKit sends text into the agent's existing terminal session. The agent decides how to respond, including whether it needs to ask for tool approval or run commands through its normal interactive flow. |
| 168 | + |
| 169 | +This is why `agent send --wait` can be a practical bridge between automation and interactive coding agents: scripts can request work, while the agent still follows its normal permission and tool behavior. |
| 170 | + |
| 171 | +## What are common failure cases? |
| 172 | + |
| 173 | +### No running agents found |
| 174 | + |
| 175 | +Start Claude Code, Codex, Gemini CLI, or OpenCode first, then run: |
| 176 | + |
| 177 | +```bash |
| 178 | +ai-devkit agent list |
| 179 | +``` |
| 180 | + |
| 181 | +### No agent found matching the id |
| 182 | + |
| 183 | +Check the displayed agent names: |
| 184 | + |
| 185 | +```bash |
| 186 | +ai-devkit agent list |
| 187 | +``` |
| 188 | + |
| 189 | +Then pass a more specific `--id`. |
| 190 | + |
| 191 | +### Cannot find terminal for the agent |
| 192 | + |
| 193 | +AI DevKit needs to find the terminal that owns the running agent process. The most reliable setups are tmux, iTerm2, and Apple Terminal. |
| 194 | + |
| 195 | +On macOS, terminal focusing and sending may require Accessibility permissions for your terminal application. |
| 196 | + |
| 197 | +### Timed out waiting for response |
| 198 | + |
| 199 | +The agent did not return to a waiting or idle state before the timeout. Increase `--timeout`, check the interactive terminal, or answer any approval prompt the agent is waiting on. |
| 200 | + |
| 201 | +## Example: shell helper function |
| 202 | + |
| 203 | +```bash |
| 204 | +ask_agent() { |
| 205 | + local agent_id="$1" |
| 206 | + shift |
| 207 | + ai-devkit agent send "$*" --id "$agent_id" --wait |
| 208 | +} |
| 209 | + |
| 210 | +ask_agent my-project "Explain the failing test and suggest the smallest fix." |
| 211 | +``` |
| 212 | + |
| 213 | +## Example: use an agent as a review step |
| 214 | + |
| 215 | +```bash |
| 216 | +review_output="$( |
| 217 | + { |
| 218 | + printf 'Review this change. Focus on bugs, regressions, and missing tests.\n\n' |
| 219 | + git diff --stat |
| 220 | + printf '\n' |
| 221 | + git diff |
| 222 | + } | ai-devkit agent send --id my-project --stdin --wait --timeout 120000 |
| 223 | +)" |
| 224 | + |
| 225 | +printf '%s\n' "$review_output" |
| 226 | +``` |
| 227 | + |
| 228 | +## Mental model |
| 229 | + |
| 230 | +`claude -p` and `codex exec` are closer to "start a new programmatic run." |
| 231 | + |
| 232 | +`ai-devkit agent send --wait` is closer to "send a message to the interactive agent I already have running, then wait until it answers." |
| 233 | + |
| 234 | +Choose the second model when session continuity matters. |
0 commit comments