Introducing a Triage Subject Matter Expert (SME) System Using OpenAI’s Agent SDK


OpenAI’s Agent SDK makes it easy to build sophisticated multi-agent ecosystems by providing simple yet powerful primitives. At its core, you work with:

• Agents – LLM-powered entities equipped with instructions and tools
• Handoffs – Allowing agents to delegate tasks to one another
• Guardrails – Enabling you to validate and control agent inputs

This post focuses on a real-world use case: creating a triage agent that dispatches questions to subject matter experts for mathematics, physics, chemistry, or geography. The system even includes guardrails to prevent inappropriate content from being processed.

The Triage SME Example

In this example, a triage agent listens for questions and forwards each query to the relevant SME agent. If none of the SMEs matches the subject, it responds that it doesn’t know the answer. Along with this, an input guardrail ensures questions do not contain unwanted content.

Below is the complete code example:


from agents import Agent, Runner, input_guardrail, GuardrailFunctionOutput, InputGuardrailTripwireTriggered
import asyncio
import re

# Define the inappropriate language guardrail
@input_guardrail
async def inappropriate_language_guardrail(ctx, agent, input):
inappropriate_words = ['stupid'] # Add inappropriate words here
pattern = re.compile('|'.join(inappropriate_words), re.IGNORECASE)
if pattern.search(input):
return GuardrailFunctionOutput(
output_info="Inappropriate language detected.",
tripwire_triggered=True
)
return GuardrailFunctionOutput(
output_info="No inappropriate language detected.",
tripwire_triggered=False
)

# Define specialized agents with customized response prefixes

math_agent = Agent(
name="Math Expert",
instructions=(
"You are the Math Subject Matter Expert (SME). "
"Preface each response with: 'This is your Math SME. The answer to your question is as follows.' "
"Then, provide the detailed answer to the math question."
)
)

physics_agent = Agent(
name="Physics Expert",
instructions=(
"You are the Physics Subject Matter Expert (SME). "
"Preface each response with: 'This is your Physics SME. The answer to your question is as follows.' "
"Then, provide the detailed answer to the physics question."
)
)

chemistry_agent = Agent(
name="Chemistry Expert",
instructions=(
"You are the Chemistry Subject Matter Expert (SME). "
"Preface each response with: 'This is your Chemistry SME. The answer to your question is as follows.' "
"Then, provide the detailed answer to the chemistry question."
)
)

geography_agent = Agent(
name="Geography Expert",
instructions=(
"You are the Geography Subject Matter Expert (SME). "
"Preface each response with: 'This is your Geography SME. The answer to your question is as follows.' "
"Then, provide the detailed answer to the geography question."
)
)

# Define the triage agent that directs questions to the appropriate SME.
triage_agent = Agent(
name="Triage Agent",
instructions=(
"You are responsible for directing questions to the appropriate subject matter expert. "
"If the question is about mathematics, hand it off to the Math Expert. "
"If it's about physics, hand it off to the Physics Expert. "
"If it's about chemistry, hand it off to the Chemistry Expert. "
"If it's about geography, hand it off to the Geography Expert. "
"If the question doesn't pertain to these subjects, respond with 'I'm sorry, I don't know the answer to that.'"
),
handoffs=[math_agent, physics_agent, chemistry_agent, geography_agent],
input_guardrails=[inappropriate_language_guardrail],
)

# Function to run the triage agent
async def main():
question = "Explain the Riemann hypothesis to a 10 year old"
try:
result = await Runner.run(triage_agent, input=question)
print(result.final_output)
except InputGuardrailTripwireTriggered:
print("Your question contains inappropriate language and cannot be processed.")

if __name__ == "__main__":
asyncio.run(main())

Running the Example

  1. Set up your development environment (on mac or unbuntu, python3.12 as an example):
    • make a directory and create virtual env
         mkdir agent_demo
      cd agent_demo
      python3 -m venv .venv
      source .venb/bin/activate
    • Install the necessary libraries:
      oython3 -m pip install openai-agents
  2. Set your OpenAI API key appropriately before running your code:
    export OPENAI_API_KEY="your_api_key_here"
  3. run the above example
    python3 example.py

What This Example Does

  • The guardrail (inappropriate_language_guardrail) scans the incoming question for any disallowed words (e.g., “stupid”). If found, it stops the process.
  • Four SME agents (math, physics, chemistry, geography) are defined, each with custom instructions that prepend a response header.
  • The triage agent reads the incoming question and, based on its content, either delegates the query to one of the SMEs or responds that it doesn’t have an answer if the question is off-topic.
  • Finally, the asynchronous main function runs the triage agent and prints the final output, showcasing the system’s capabilities.

This example illustrates how OpenAI’s Agent SDK can help developers easily build a scalable, modular multi-agent system for real-world applications without sacrificing ease of use or flexibility. Enjoy experimenting with and extending the system!


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC