Building Teams
Define team members, roles, and structure for multi-agent coordination.
Start simple: a model, team members, and instructions. Add functionality as needed.
Minimal Example
1from kern.team import Team2from kern.agent import Agent3from kern.models.openai import OpenAIResponses4from kern.tools.hackernews import HackerNewsTools5from kern.tools.yfinance import YFinanceTools67news_agent = Agent(8 name="News Agent",9 role="Get trending tech news from HackerNews",10 tools=[HackerNewsTools()]11)1213finance_agent = Agent(14 name="Finance Agent",15 role="Get stock prices and financial data",16 tools=[YFinanceTools()]17)1819team = Team(20 name="Research Team",21 members=[news_agent, finance_agent],22 model=OpenAIResponses(id="gpt-4o"),23 instructions="Delegate to the appropriate agent based on the request."24)2526team.print_response("What are the trending AI stories and how is NVDA stock doing?", stream=True)Team Modes
Teams default to coordinate mode (leader delegates and synthesizes). Set mode to change how the leader collaborates with members.
1from kern.team.mode import TeamMode2from kern.models.openai import OpenAIResponses34team = Team(5 name="Language Router",6 members=[...],7 model=OpenAIResponses(id="gpt-4o"),8 mode=TeamMode.route9)| Mode | Configuration | Use case |
|---|---|---|
| Coordinate | mode=TeamMode.coordinate (default) | Decompose work, delegate to members, synthesize results |
| Route | mode=TeamMode.route | Route to a single specialist and return their response directly |
| Broadcast | mode=TeamMode.broadcast | Delegate the same task to all members and synthesize |
| Tasks | mode=TeamMode.tasks | Run a task list loop until the goal is complete |
Tasks mode runs an iterative task loop. Use max_iterations to cap how many cycles the leader can run.
1from kern.models.openai import OpenAIResponses23team = Team(4 name="Ops Team",5 members=[...],6 model=OpenAIResponses(id="gpt-4o"),7 mode=TeamMode.tasks,8 max_iterations=69)Team Members
Each member should have a name and role. The team leader uses these to decide who handles what.
1news_agent = Agent(2 name="News Agent", # Identifies the agent3 role="Get trending tech news from HackerNews", # Tells the leader what this agent does4 tools=[HackerNewsTools()]5)For better tracing, also set an id:
1news_agent = Agent(2 id="news-agent",3 name="News Agent",4 role="Get trending tech news from HackerNews",5 tools=[HackerNewsTools()]6)When both id and name are set on a member, team delegation uses id as the member identifier.
Members must be native Kern Agent or Team instances. External-framework
adapters (ClaudeAgent, LangGraphAgent, DSPyAgent) can be served by
AgentOS as standalone agents but cannot be a member agent of an Kern Team or an agent of a Workflow Step yet. See
Multi-Framework Support.
Nested Teams
Teams can contain other teams. The top-level leader delegates to sub-team leaders, who delegate to their members.
1from kern.team import Team2from kern.agent import Agent34team = Team(5 name="Language Team",6 members=[7 Agent(name="English Agent", role="Answer in English"),8 Agent(name="Chinese Agent", role="Answer in Chinese"),9 Team(10 name="Germanic Team",11 role="Handle German and Dutch questions",12 members=[13 Agent(name="German Agent", role="Answer in German"),14 Agent(name="Dutch Agent", role="Answer in Dutch"),15 ],16 ),17 ],18)Model Inheritance
Team members inherit the model from their parent team if not explicitly set.
1from kern.team import Team2from kern.agent import Agent3from kern.models.openai import OpenAIResponses4from kern.models.anthropic import Claude56# This agent uses its own model (Claude)7agent_with_model = Agent(8 name="Claude Agent",9 model=Claude(id="claude-sonnet-4-5"),10 role="Research with Claude"11)1213# This agent inherits gpt-4o from the team14agent_without_model = Agent(15 name="Inherited Agent",16 role="Research with inherited model"17)1819team = Team(20 name="Research Team",21 model=OpenAIResponses(id="gpt-4o"), # Default for team and members without a model22 members=[agent_with_model, agent_without_model]23)Callable Factories
Pass a function instead of a static list for members, tools, or knowledge. The function is called at the start of each run, so the composition can vary per user or session.
1from kern.agent import Agent2from kern.models.openai import OpenAIResponses3from kern.team import Team45writer = Agent(6 name="Writer",7 role="Content writer",8 model=OpenAIResponses(id="gpt-5-mini"),9 instructions=["Write clear, concise content."],10)1112researcher = Agent(13 name="Researcher",14 role="Research analyst",15 model=OpenAIResponses(id="gpt-5-mini"),16 instructions=["Research topics and summarize findings."],17)181920def pick_members(session_state: dict):21 if session_state.get("needs_research", False):22 return [researcher, writer]23 return [writer]242526team = Team(27 name="Content Team",28 model=OpenAIResponses(id="gpt-5-mini"),29 members=pick_members,30 cache_callables=False,31)3233team.print_response(34 "Write a haiku about Python",35 session_state={"needs_research": False},36 stream=True,37)3839team.print_response(40 "Research the history of Python and write a short summary",41 session_state={"needs_research": True},42 stream=True,43)The same pattern works for tools and knowledge. Agents also support callable factories for tools and knowledge.
Injected Parameters
Name your factory function parameters to receive context automatically:
| Parameter | Type | Description |
|---|---|---|
agent | Agent | The current Agent instance |
team | Team | The current Team instance |
run_context | RunContext | Run context with user_id, session_id, session_state |
session_state | dict | Session state dict directly (defaults to {} if None) |
Use any combination. A zero-argument factory also works.
add_tool and set_tools
add_tool() raises RuntimeError when tools is a callable factory. Use set_tools() to replace the factory entirely:
1team = Team(tools=tools_for_user, ...)23team.add_tool(some_tool) # raises RuntimeError45team.set_tools(new_factory) # replace with a new factory6team.set_tools([DuckDuckGoTools()]) # replace with a static listCallable Caching Settings
Factory results are cached by default. The cache key is resolved in this order: custom key function > user_id > session_id. If none are available, caching is skipped and the factory runs every time.
| Setting | Default | Description |
|---|---|---|
cache_callables | True | Enable or disable caching for all callable factories |
callable_tools_cache_key | None | Custom cache key function for tools factory |
callable_knowledge_cache_key | None | Custom cache key function for knowledge factory |
callable_members_cache_key | None | Custom cache key function for members factory (Team only) |
Set cache_callables=False when session_state changes between runs and the factory should re-evaluate each time.
Clear cached results programmatically:
1from kern.utils.callables import clear_callable_cache23clear_callable_cache(team) # Clear all caches4clear_callable_cache(team, kind="tools") # Clear tools cache only5clear_callable_cache(team, kind="tools", close=True) # Clear and call .close() on cached resourcesUse aclear_callable_cache() in async code.
Team Features
Teams support the same features as agents:
| Feature | Description |
|---|---|
| Instructions | Guide the team leader on how to coordinate |
| Mode | Choose the coordination strategy (coordinate, route, broadcast, tasks) |
| Database | Persist session history and state |
| Reasoning | Enable the leader to plan before delegating |
| Knowledge | Give the leader access to a knowledge base |
| Memory | Store and recall information across sessions |
| Tools | Give the leader tools to use directly |
| Skills | Give the leader domain expertise via instructions, scripts, and references |
See the guides below to add these features.
Next Steps
| Task | Guide |
|---|---|
| Run teams | Running Teams |
| Control delegation | Delegation |
| Add skills | Team Skills |
| Add chat history | Chat History |
| Manage sessions | Sessions |
| Handle input/output | Input and Output |
| Add knowledge | Knowledge |
| Add guardrails | Guardrails |