# Child invocations

`startAgentInvocation()` starts a child Agent and returns a controller. With
it, trusted code (a parent Agent, a Capability tool, or host code) can inspect
the child, cancel it, or send it input while it runs. The controller gives no
final result to await.

Use [`runAgent()`](https://vitehub.dev/docs/agents/invocations) when you only need the result.
`runAgent()` runs the child inline and returns `[error, result]` when it
finishes. The controller
from `startAgentInvocation()` does not return a final result that you can
await. It gives control and inspection.

## Start and inspect a child

```ts fragment
import { startAgentInvocation } from 'vite-hub/agent'
import researcher from '../agents/researcher'

const child = await startAgentInvocation(researcher, runtimeContext, {
  prompt: 'Compare the two deployment options.',
})

const current = await child.inspect()
if (current.outcome === 'available') {
  console.log(current.invocation.id, current.invocation.status)
}
```

Each start gets a new, stable `child.id`. `inspect()` returns an `available`
snapshot with state `pending`, `running`, `completed`, `failed`, or
`cancelled`, or an explicit `unavailable` outcome.

An inline or serverless child can become unavailable after its process ends.
A Workflow-backed child reads from its Workflow Run while you keep the
controller. ViteHub keeps no controller registry, so you cannot look one up
by id later.

## Cancel active work

```ts fragment
const cancellation = await child.cancel('The parent no longer needs this work.')

if (cancellation.outcome === 'accepted') {
  const latest = await child.inspect()
}
```

`accepted` means the runtime took the request. Inspect again to see the end
state. A provider can return `unsupported`. A finished child returns
`invalid-state`.

## Respond to provider requests

Check `child.support` before you send input. Check the result too, because
support changes with the lifecycle state.

```ts fragment
if (child.support.respond) {
  const result = await child.sendInput(
    { messages: [responseMessage] },
    { mode: 'respond' },
  )
}
```

| Mode | Use it to | Support |
| --- | --- | --- |
| `'respond'` | Answer a pending provider approval or a `data-agent-input` question. | Inline provider runtimes, while the request is pending. |
| `'steer'` | Add text to the active provider turn, as a string or a text-only `Message[]`. | When `child.support.steer` is `true`. |
| `'follow-up'` | Start another turn. | Not supported yet. |

Steering returns:

- `accepted` when the provider adds the input to the active turn.
- `unsupported` when the provider cannot do it safely, for example for input with attachments.
- `invalid-state` for an unclear submission or a cancel failure. Do not resend that input automatically.

Workflow-backed children do not accept input yet, because their adapters lack
the same order and lifecycle guarantees.

## Keep child selection in trusted code

The model can call a named app tool. Trusted code picks the Agent Definition,
and ViteHub gives the child id. Do not let model input choose which Agent runs.

When a tool uses `runAgent()` instead, check `error` before it reads
`result.text` or `result.output`.

## Hand off input resources

If your host creates input resources, such as uploaded files, pass
`onInputHandoff` in the fourth argument:

```ts fragment
const child = await startAgentInvocation(researcher, runtimeContext, input, {
  onInputHandoff() {
    markUploadsAsOwnedByRuntime()
  },
})
```

This synchronous callback runs just before input can reach the inline runtime
or the Workflow provider.

- If the start fails before the callback, the host can remove the new resources.
- After the callback, the runtime can keep the input even if the start fails.
  Keep the resources until you know who owns them.

The callback does not confirm durable acceptance or replace cleanup after a
process interruption.

<!-- vitehub-docs:section-links -->
## In this section

- [Start](https://vitehub.dev/raw/docs/agents.md)
- [Define and run](https://vitehub.dev/raw/docs/agents/agent-definitions.md)
- [Instructions and tools](https://vitehub.dev/raw/docs/agents/instructions.md)
- [History and context](https://vitehub.dev/raw/docs/agents/chat-history-sessions.md)
- [Channels](https://vitehub.dev/raw/docs/agents/channels.md)
- [Inspect and evaluate](https://vitehub.dev/raw/docs/agents/observability.md)
- Advanced
  - [Agent presets and inheritance](https://vitehub.dev/raw/docs/agents/agent-presets.md)
  - [Invocation storage](https://vitehub.dev/raw/docs/agents/invocation-storage.md)
  - [Runtime policy, approvals, and traces](https://vitehub.dev/raw/docs/agents/runtime-policy.md)
  - [Custom capabilities](https://vitehub.dev/raw/docs/agents/capabilities/custom.md)
  - [Child invocations](https://vitehub.dev/raw/docs/agents/controlled-child-invocations.md) (this page)
  - [Sandboxes](https://vitehub.dev/raw/docs/agents/sandboxes.md)
  - [Sandbox tutorial](https://vitehub.dev/raw/docs/agents/sandbox-tutorial.md)
  - [Process hosts](https://vitehub.dev/raw/docs/agents/process-hosts.md)
- [Recipes](https://vitehub.dev/raw/docs/agents/support-agent.md)
- [Presets](https://vitehub.dev/raw/docs/agents/presets.md)
- [Capabilities](https://vitehub.dev/raw/docs/agents/capabilities/access.md)

Next: [AGENT_R0074](https://vitehub.dev/raw/docs/agents/diagnostics/agent_r0074.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
