1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Example 13: Agent Tool - Subagent Spawning
*
* Demonstrates using the Agent tool to spawn subagents for complex
* multi-step tasks. When the Agent tool is called, it spawns a new
* agent that runs independently to complete the sub-task.
*
* Run: cargo run --example 13_agent_tool
*
* Environment variables from .env:
* - AI_BASE_URL: LLM server URL
* - AI_AUTH_TOKEN: API authentication token
* - AI_MODEL: Model name (defaults to claude-sonnet-4-6)
*/
use ai_agent::{Agent, EnvConfig, get_all_tools};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("--- Example 13: Agent Tool ---\n");
// Load config from .env
let config = EnvConfig::load();
let model = config
.model
.unwrap_or_else(|| "claude-sonnet-4-6".to_string());
println!("Using model: {}\n", model);
// Create agent with all tools including Agent tool
let tools = get_all_tools();
println!(
"Available tools: {:?}\n",
tools.iter().map(|t| &t.name).collect::<Vec<_>>()
);
let agent = Agent::new(&model).max_turns(10).tools(tools);
// Set system prompt to encourage tool use
agent.set_system_prompt(
"You have access to tools. When asked to spawn a subagent, \
use the 'Agent' tool with the appropriate description and prompt.",
);
// The main agent will use the Agent tool to spawn a subagent
let result = agent
.query(
"Use the 'Agent' tool to spawn a subagent. Description: 'count-numbers'. \
Prompt: 'Count from 1 to 3, one number per line.'",
)
.await?;
println!("{}", result.text);
println!("\n=== done ===");
Ok(())
}