๐ค Structured Tool Output
Structured tool output is about making AI agents produce their results, actions, and observations in a consistent, standardized format that both humans and machines can reliably parse. Instead of free-form text, the agent outputs responses following a defined schema โ action schemas, observation formats, and response templates.
Think of it like a doctor's report. A casual description of symptoms is hard to process. A structured form with fields for diagnosis, symptoms, treatment, and follow-up is easy to read and act on.
๐ฏ Why This Mattersโ
When AI agents interact with other systems (or other agents), unstructured output causes problems:
- Parsing failures โ Code can't reliably extract data from free-form text
- Inconsistency โ Different responses use different formats, breaking automation
- Lost information โ Important details get buried in paragraphs of text
- Debugging difficulty โ Hard to trace what the agent did and why
Structured output solves all of this by enforcing predictable, machine-readable formats for every agent response.
๐ The Concept in Detailโ
Action Schemasโ
An action schema defines what every agent action must look like:
{
"thought": "Why I'm taking this action",
"action": "tool_name",
"parameters": {
"param1": "value1",
"param2": "value2"
},
"expected_outcome": "What I expect to happen"
}
Observation Schemasโ
After an action, the observation should also be structured:
{
"action_taken": "tool_name",
"status": "success | failure | partial",
"result": "The actual output from the tool",
"key_findings": ["Finding 1", "Finding 2"],
"next_step": "What to do based on this result"
}
Standardized Response Formatsโ
| Format Type | Use Case | Benefit |
|---|---|---|
| JSON | Machine-to-machine | Easily parsed by code |
| Markdown Tables | Human-readable reports | Clear and organized |
| YAML | Configuration-style output | Readable and structured |
| Key-Value Pairs | Simple structured data | Fast to scan |
| XML Tags | Labeled sections | Easy to extract sections |
The Full Agent Output Cycleโ
INPUT โ THOUGHT โ ACTION โ OBSERVATION โ RESPONSE
Each stage follows its own schema for consistency.
๐ก Prompt Examplesโ
Complete Agent Output Schemaโ
You are a research agent. For EVERY response, follow this exact
output format:
=== AGENT RESPONSE ===
THOUGHT:
reasoning: [Why you're taking this action]
confidence: [low | medium | high]
ACTION:
type: [search | analyze | summarize | report]
target: [What you're acting on]
parameters: [Any specific parameters]
OBSERVATION:
status: [success | failure | needs_more_info]
findings:
- [Finding 1]
- [Finding 2]
PROGRESS:
completed: [List of completed tasks]
remaining: [List of remaining tasks]
percentage: [Estimated % complete]
NEXT_STEP:
action: [What to do next]
reason: [Why this is the logical next step]
=== END RESPONSE ===
Never respond in free-form text. Always use this schema.
JSON Agent Outputโ
You are an autonomous task agent. Respond ONLY in JSON format.
Every response must follow this exact structure:
```json
{
"iteration": 1,
"thought": {
"analysis": "What I understand about the current state",
"plan": "What I intend to do and why"
},
"action": {
"type": "search | calculate | write | ask_user",
"details": "Specific action description",
"parameters": {}
},
"observation": {
"result": "What happened after the action",
"success": true,
"key_data": []
},
"status": {
"goal_progress": "0-100%",
"tasks_completed": [],
"tasks_remaining": [],
"blockers": []
},
"next_action": "Description of next planned step"
}
Increment the iteration number with each response. If the goal is complete, add "goal_complete": true to the status.
---
## โ Bad Example
```text
You are a research agent. Look into the top JavaScript frameworks
and tell me about them. Give me lots of detail.
Why it fails: No output format defined. The AI will produce a long, unstructured essay. You can't reliably parse it, compare items, or feed it into another system. Different runs will produce different formats.
โ Improved Exampleโ
You are a research agent. Analyze the top 5 JavaScript frameworks.
OUTPUT SCHEMA (follow exactly):
For each framework, produce this structured block:
FRAMEWORK:
name: [Framework name]
version: [Latest stable version]
category: [frontend | backend | fullstack]
METRICS:
github_stars: [Approximate number]
npm_weekly_downloads: [Approximate number]
learning_curve: [easy | moderate | steep]
ANALYSIS:
strengths:
- [Strength 1]
- [Strength 2]
- [Strength 3]
weaknesses:
- [Weakness 1]
- [Weakness 2]
best_for: [Use case description]
avoid_when: [When NOT to use it]
---
After all 5 frameworks, provide:
COMPARISON_TABLE:
| Framework | Stars | Downloads | Learning Curve | Best For |
|-----------|-------|-----------|---------------|----------|
| ... | ... | ... | ... | ... |
RECOMMENDATION:
top_pick: [Framework name]
reason: [1-2 sentence justification]
runner_up: [Framework name]
Why it works: Every piece of output has a defined location and format. Results are consistent across runs. The data can be parsed, compared, and fed into other systems. The comparison table provides a quick summary.
๐งช Try It Yourselfโ
๐งช Try It Yourself
Edit the prompt and click Run to see the AI response.
๐๏ธ Practice Challengeโ
Design a structured output schema for an AI bug-fixing agent. The agent should output:
- Bug Analysis โ What the bug is, where it is, severity
- Root Cause โ Why it happened
- Fix Plan โ Steps to fix it
- Code Changes โ Before/after code blocks
- Test Plan โ How to verify the fix works
- Risk Assessment โ Could this fix break anything else?
Write the full prompt with the output schema and a sample bug to analyze.
๐ Real-World Scenarioโ
Scenario: You're building an AI agent that monitors your e-commerce platform and produces hourly reports.
Without structured output, each report looks different and you can't automate trend analysis. With structured output:
HOURLY_REPORT:
timestamp: "2025-01-15T14:00:00Z"
metrics:
orders: 142
revenue: 8450.00
avg_order_value: 59.51
cart_abandonment_rate: 0.23
alerts:
- type: "high_abandonment"
severity: "warning"
message: "Cart abandonment 15% above baseline"
trend:
direction: "declining"
compared_to_yesterday: "-12%"
recommended_actions:
- "Review checkout flow for errors"
- "Send cart recovery emails"
This structured format can be automatically ingested by dashboards, trigger alerts, and feed into decision-making systems.
๐ค Interview Questionโ
Q: Why is structured output important for AI agents, and how do you enforce it?
A: Structured output is critical because AI agents often need to interface with other systems, other agents, or automated pipelines. Unstructured text is unpredictable and hard to parse. To enforce structured output, you: 1) Define an explicit schema in the prompt with exact field names, types, and formats. 2) Use a clear directive like "respond ONLY in this format." 3) Provide an example of a correctly formatted response. 4) Include validation rules (e.g., "status must be one of: success, failure, partial"). For production systems, you can also use JSON Schema validation on the output to catch formatting errors and retry if the format is wrong.
๐ Summaryโ
- Structured tool output makes agent responses predictable and machine-readable
- Define action schemas (thought + action + parameters) and observation schemas (result + status + findings)
- Use formats like JSON, YAML, or labeled sections instead of free-form text
- Every agent response should include progress tracking (what's done, what's left)
- Structured output enables automation โ other systems can parse and act on agent results
- Always provide the exact schema in your prompt and instruct the AI to follow it strictly
- Consistency in output format is what makes AI agents production-ready