Direct Response Mode
Route requests to specialized agents who respond directly.
Use mode=TeamMode.route to route requests to the appropriate agent and return the member response directly. The legacy respond_directly=True flag still works, but mode is preferred.
This example creates a language router with three agents:
- English Agent - Responds in English
- Japanese Agent - Responds in Japanese
- Spanish Agent - Responds in Spanish
Create a Python file
1from kern.agent import Agent2from kern.models.openai import OpenAIResponses3from kern.team.team import Team4from kern.team.mode import TeamMode56english_agent = Agent(7 name="English Agent",8 role="You only answer in English",9 model=OpenAIResponses(id="gpt-5.2"),10)11japanese_agent = Agent(12 name="Japanese Agent",13 role="You only answer in Japanese",14 model=OpenAIResponses(id="gpt-5.2"),15)16spanish_agent = Agent(17 name="Spanish Agent",18 role="You only answer in Spanish",19 model=OpenAIResponses(id="gpt-5.2"),20)2122language_router = Team(23 name="Language Router",24 model=OpenAIResponses(id="gpt-5.2"),25 mode=TeamMode.route,26 members=[english_agent, japanese_agent, spanish_agent],27 instructions=[28 "Route questions to the appropriate language agent.",29 "If the language is not supported, respond in English.",30 ],31 markdown=True,32 show_members_responses=True,33)3435# English36language_router.print_response("How are you?", stream=True)3738# Japanese39language_router.print_response("お元気ですか?", stream=True)4041# Spanish42language_router.print_response("¿Cómo estás?", stream=True)Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall dependencies
1uv pip install -U kern-ai openaiExport your OpenAI API key
1export OPENAI_API_KEY="your_openai_api_key_here"1$Env:OPENAI_API_KEY="your_openai_api_key_here"Run Team
1python respond_directly.py