AI Agents in Depth
How Kestra's AIAgent task type works: system messages, prompts, tools, memory, and the agent loop. How to compose multi-agent systems in Kestra. How human-in-the-loop integrates with agent workflows. The production constraints and what's still hard.
The AIAgent task type
Kestra's AI Agent plugin (io.kestra.plugin.ai.agent.AIAgent) provides autonomous orchestration capabilities within a flow. The core configuration:
```yaml
tasks:
- id: research_agent
type: io.kestra.plugin.ai.agent.AIAgent
systemMessage: |
You are a research assistant. Your job is to answer questions
about companies using publicly available information.
Always cite your sources. If you cannot find reliable information,
say so rather than speculating.
prompt: "{{ inputs.research_query }}"
```
System message: defines the agent's role, behavioral constraints, and operating guidelines. This is the "character" of the agent — what it's supposed to do and what it must not do. Well-crafted system messages are the primary quality lever for agent behavior.
Prompt: the specific input for this agent execution. Often dynamically rendered from Pebble expressions, allowing the same agent definition to serve different queries.
Provider configuration via Plugin Defaults:
To avoid repeating LLM provider configuration in every agent task, Kestra recommends using plugin defaults at the namespace level:
```yaml
pluginDefaults:
- type: io.kestra.plugin.ai
values:
provider:
type: ANTHROPIC
apiKey: "{{ secret('ANTHROPICAPIKEY') }}"
modelName: claude-sonnet-4-5
```
This sets the provider for all AI plugin tasks in the namespace — change the model once to affect all agents.
A studied pattern of play — a proven sequence for a local situation. A well-defined AIAgent system message is jōseki: a proven behavioral pattern that produces reliable results in the local context.
Tools, memory, and the agent loop
Tools the agent can use:
Kestra's AI Agent has access to built-in tools that extend what the agent can do beyond reasoning:
- Web search: the agent can query the web for current information
- Task execution: the agent can trigger other Kestra tasks (run a Python script, call an API, query a database) as part of its decision-making
- Flow calling: the agent can trigger other Kestra flows as subflows, enabling multi-agent systems where specialized agents are invoked on demand
The agent loop:
The agent runs an iterative perceive → reason → act → observe cycle:
1. Receive the prompt (perceive)
2. Call the LLM to decide what to do next (reason)
3. Execute a tool if the LLM decides to use one (act)
4. Observe the tool's output
5. Pass the output back to the LLM with updated context (perceive again)
6. Repeat until the LLM produces a final answer or a termination condition is met
Memory:
Kestra's AI Agent supports memory — context that persists across agent steps within a single execution. The memory allows the agent to build on earlier steps: "I found X in step 2, so in step 5 I should check Y." Without memory, each LLM call starts fresh and the agent can't reason across steps.
What controls the loop:
- Max iterations: prevent runaway agent loops that consume tokens indefinitely
- Termination conditions: the agent signals completion by producing a final answer in a defined format
- Kestra's flow-level timeout: the entire agent task is subject to Kestra's task timeout, providing a hard cost/time ceiling
Multi-agent systems and human-in-the-loop
Multi-agent patterns in Kestra:
Because an agent can trigger other flows as subflows, multi-agent systems emerge naturally:
*Sequential:* Agent A runs a research task, its output flows into Agent B which runs an analysis task, Agent C writes the report. Each is a separate AIAgent task in the same flow.
*Fan-out:* A coordinator agent receives a complex request, breaks it into subtasks, and uses EachParallel to run specialized agents in parallel — each one a separate AIAgent in a separate subflow. The coordinator waits for all to complete and aggregates results.
*Router pattern:* a lightweight classification step (or a simple AIAgent) determines which specialized agent to invoke based on the nature of the request — a Switch flowable task routes to the appropriate agent.
Human-in-the-loop with AI agents:
Kestra's Pause task integrates naturally with agent workflows:
```yaml
tasks:
- id: agent_draft
type: io.kestra.plugin.ai.agent.AIAgent
prompt: "Draft a response to this customer complaint: {{ inputs.complaint }}"
- id: human_review
type: io.kestra.core.tasks.flows.Pause
timeout: PT24H
onTimeout: FAIL
inputs:
- id: approved
type: BOOLEAN
- id: edited_response
type: STRING
required: false
- id: send_response
type: io.kestra.plugin.smtp.Email
to: "{{ inputs.customer_email }}"
body: "{{ outputs.humanreview.editedresponse ?? outputs.agent_draft.output }}"
```
The Pause task blocks the flow execution, sends a notification (email, Slack), and waits up to 24 hours for a human to review the agent's draft and either approve it or submit an edited version. The flow then uses whichever response is available. If no human responds within 24 hours, the flow fails (or an alternative path can be defined).
Select any text in the lesson to ask a question. Press Esc to close.