You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enhance TinyAgent with Subagent Tools and Anthropic Prompt Caching
This commit introduces a revolutionary subagent system for TinyAgent, enabling parallel task execution with context isolation. Key features include specialized subagents for various tasks, a comprehensive configuration system, and automatic resource management. Additionally, the Anthropic prompt caching mechanism is integrated, optimizing API costs by caching large messages for Claude models. Documentation and examples are updated to reflect these enhancements, providing users with clear guidance on utilizing the new features effectively.
Copy file name to clipboardExpand all lines: README.md
+219-3Lines changed: 219 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,9 +31,10 @@ Inspired by:
31
31
## Overview
32
32
This is a tiny agent framework that uses MCP and LiteLLM to interact with language models. You have full control over the agent, you can add any tools you like from MCP and extend the agent using its event system.
33
33
34
-
**Two Main Components:**
34
+
**Three Main Components:**
35
35
-**TinyAgent**: Core agent with MCP tool integration and extensible hooks
36
-
-**TinyCodeAgent**: Specialized agent for secure Python code execution with pluggable providers
36
+
-**TinyCodeAgent**: Specialized agent for secure Python code execution with pluggable providers
37
+
-**Subagent Tools**: Revolutionary parallel task execution system with context isolation and specialized workers
37
38
38
39
## Installation
39
40
@@ -258,6 +259,206 @@ Each checkpoint includes:
258
259
259
260
For detailed documentation, see the [TinyCodeAgent README](tinyagent/code_agent/README.md).
The subagent system enables you to create specialized AI workers that can execute tasks in parallel with complete context isolation. Each subagent operates independently with its own conversation history, resource management, and cleanup.
265
+
266
+
### Quick Start with Subagents
267
+
268
+
```python
269
+
import asyncio
270
+
from tinyagent import TinyAgent
271
+
from tinyagent.tools.subagent import create_general_subagent, create_coding_subagent
272
+
273
+
asyncdefmain():
274
+
# Create main agent
275
+
main_agent = TinyAgent(
276
+
model="gpt-4o-mini",
277
+
api_key="your-api-key"
278
+
)
279
+
280
+
# Add a general-purpose subagent
281
+
helper = create_general_subagent(
282
+
name="helper",
283
+
model="gpt-4.1-mini",
284
+
max_turns=15,
285
+
enable_python=True,
286
+
enable_shell=True
287
+
)
288
+
main_agent.add_tool(helper)
289
+
290
+
# Add a specialized coding subagent
291
+
coder = create_coding_subagent(
292
+
name="coder",
293
+
model="claude-3-sonnet",
294
+
max_turns=25
295
+
)
296
+
main_agent.add_tool(coder)
297
+
298
+
# Use subagents in parallel
299
+
result =await main_agent.run("""
300
+
I need help with a Python project:
301
+
1. Use coder to implement a binary search algorithm
302
+
2. Use helper to create unit tests for it
303
+
3. Use helper to benchmark the performance
304
+
305
+
Make sure both tasks run efficiently and provide comprehensive results.
306
+
""")
307
+
308
+
print(result)
309
+
310
+
asyncio.run(main())
311
+
```
312
+
313
+
### Specialized Subagent Types
314
+
315
+
The subagent system provides pre-configured factories for common use cases:
316
+
317
+
```python
318
+
from tinyagent.tools.subagent import (
319
+
create_research_subagent,
320
+
create_coding_subagent,
321
+
create_analysis_subagent,
322
+
create_writing_subagent,
323
+
create_planning_subagent
324
+
)
325
+
326
+
# Research subagent - optimized for information gathering
327
+
researcher = create_research_subagent(
328
+
name="researcher",
329
+
model="gpt-4o",
330
+
max_turns=20
331
+
)
332
+
333
+
# Coding subagent - with Python/shell execution
334
+
coder = create_coding_subagent(
335
+
name="coder",
336
+
model="claude-3-sonnet",
337
+
local_execution=True,
338
+
timeout=300# 5 minute timeout
339
+
)
340
+
341
+
# Analysis subagent - for data analysis tasks
342
+
analyst = create_analysis_subagent(
343
+
name="analyst",
344
+
model="gpt-4.1-mini",
345
+
enable_python_tool=True
346
+
)
347
+
348
+
# Writing subagent - for content creation
349
+
writer = create_writing_subagent(
350
+
name="writer",
351
+
model="claude-3-haiku",
352
+
temperature=0.3
353
+
)
354
+
355
+
# Planning subagent - for strategy and planning
356
+
planner = create_planning_subagent(
357
+
name="planner",
358
+
model="gpt-4o",
359
+
max_turns=15
360
+
)
361
+
362
+
# Add all subagents to your main agent
363
+
for subagent in [researcher, coder, analyst, writer, planner]:
364
+
main_agent.add_tool(subagent)
365
+
```
366
+
367
+
### Advanced Configuration with Parent Inheritance
368
+
369
+
Subagents can automatically inherit configuration from their parent agent:
370
+
371
+
```python
372
+
from tinyagent.tools.subagent import SubagentConfig, create_subagent_tool
373
+
374
+
# Create main agent with callbacks and configuration
375
+
main_agent = TinyAgent(
376
+
model="gpt-4o-mini",
377
+
api_key="your-key",
378
+
log_manager=my_log_manager,
379
+
session_id="main-session"
380
+
)
381
+
382
+
# Create configuration that inherits from parent
383
+
config = SubagentConfig.from_parent_agent(
384
+
parent_agent=main_agent, # Inherits API keys, logging, session info
385
+
model="claude-3-sonnet", # Override specific parameters
386
+
max_turns=20,
387
+
enable_python_tool=True,
388
+
timeout=300, # 5 minute timeout
389
+
working_directory="/tmp/subagent"
390
+
)
391
+
392
+
# Create custom subagent with inherited configuration
393
+
specialized_tool = create_subagent_tool(
394
+
name="specialist",
395
+
config=config,
396
+
description="A specialized agent for complex analysis tasks"
397
+
)
398
+
main_agent.add_tool(specialized_tool)
399
+
```
400
+
401
+
### Custom Agent Factories
402
+
403
+
For maximum flexibility, use custom agent factories to create any type of agent:
404
+
405
+
```python
406
+
from tinyagent.tools.subagent import SubagentConfig, create_subagent_tool
407
+
from tinyagent import TinyCodeAgent
408
+
409
+
defmy_custom_factory(**kwargs):
410
+
"""Custom factory for creating specialized agents."""
|**Timeout Handling**| Basic | Advanced with cleanup |
460
+
|**Configuration**| Static | Dynamic with inheritance |
461
+
261
462
## How the TinyAgent Hook System Works
262
463
263
464
TinyAgent is designed to be **extensible** via a simple, event-driven hook (callback) system. This allows you to add custom logic, logging, UI, memory, or any other behavior at key points in the agent's lifecycle.
0 commit comments