Fun with Agents - Simple example of an Agent Hand-off

I have coded this example before using the Assistants API. It was easy to adapt it to the Agent SDK. This is the simplest example of an agent hand off I could think of. The first agent is a poet, there are three analyst agents each of whom specialize in a style of poetry. Finally there is a triage agent which must decide which analyst should provide feedback. I find it’s useful and more fun to have the agents introduce themselves and to give them some personality. I could see extending this into a gaming scenario instead of poetry.

import os
os.environ["OPENAI_API_KEY"] = 'your API goes here'

from agents import Agent, Runner

"""
 A simple example of an agent hand off 
   (1) A poet writes a poem.
   (2) A triage agent selects the right analyst to provide feedback based only 
   on the style of poem.
   (3) There are three analysts each a specialist in a type of poem
   (4) The selected analyst provides the feedback 
"""

useModel = "gpt-4o"

Poet_Agent = Agent(
    name="Poet",
    instructions="You are an expert at writing poetry",
    model=useModel,
)

Analyst_Agent1 = Agent(
    name="Haiku-Analyst",
    instructions="""Always introduce yourself as 'Helen the Haiku Whisperer'. You are expert in analyzing poetry. 
    You are specialize in Haikus. Your output is poetic but concise""" ,
    model=useModel,
)


Analyst_Agent2 = Agent(
    name="Free-Verse-Analyst",
    instructions="""Always introduce yourself as 'Fred the Free Verse Expert'. 
    You are expert in analyzing poetry. You specialize in non-rhyming free verse poems. 
    Your output is quirky, fun and kind""",
    model=useModel,
)

Analyst_Agent3 = Agent(
    name="Rhyming-Poem-Analyst",
    instructions="""Always introduce yourself as 'Ron the Rhyme Master', 
    You are expert in analyzing poetry. You specialize in classic rhyming poems. 
    Your analyst is direct and to the point. You spare no ones feelings and can be quite verbose""",
    model=useModel,
)

triage_agent = Agent(
    name="triage_agent",
    instructions= """We are going to write a poem, there will be three agents that serve as analysts. The Poet will write poem and based on the style of poem you will hand off to appropriate analyst """,
    handoffs=[Analyst_Agent1,Analyst_Agent2,Analyst_Agent3],
    model=useModel
)

'''
Write the initial poem in the style of your choice 
'''

style_of_Poem ="Ballad with dark themes"

result = await Runner.run(
    starting_agent= Poet_Agent,
    input= f""""Identify yourself as the Poet. You are going to write a poem about the plight of homelessness in the city during Winter. 
You will write this poem as a {style_of_Poem}. 
Think about this step by step. First describe your approach to the poem, then write the poem."""
)
print('\n',result.final_output) 

Draft_Poem = result.final_output

result = await Runner.run(
    starting_agent= triage_agent,
    input= f"""Here is the first draft from the poet ... {Draft_Poem}. 
    Determine the best analyst to hand this poem off to for analysis"""
)
print('\n',result.final_output) 

print("\n All done. Carry on ")

Some sample output is shown below. I asked for a ballad, to test whether the triage agent would correctly select the right analyst, amongst haiku, free-verse and classic rhyming specialists – it did.


### Approach

When crafting a ballad about the plight of homelessness in the city during winter, I want to create a narrative that evokes empathy and highlights the stark contrast between the warmth of home and the coldness of the streets. Ballads often tell a story and have a rhythmic, musical quality, so I’ll focus on a structure with quatrains and a rhyme scheme (ABCB).

I’ll employ imagery that captures the harshness of winter and the vulnerability of those without shelter. Dark themes will be conveyed through the setting, symbolic elements like cold wind and bare branches, and the emotional tone of despair and resilience.

### The Poem

**In Winter's Grasp**

Upon the city's shadowed streets,  
Where bitter winds do moan,  
The nameless tread with weary feet,  
Their hearts as cold as stone.

Beneath the sky's indifferent eye,  
The snow descends like tears,  
It blankets all, as dreams belie,  
The haunting chill of fears.

Each corner holds a silent tale,  
Of lives left out to freeze,  
In cardboard beds where hopes grow frail,  
Midst whispers of the trees.

The moon casts down its silver light,  
On souls that darkness keeps,  
A fleeting warmth in endless night,  
Where only silence weeps.

Yet still they walk, these shadows thin,  
Through alleyways of steel,  
In search of warmth, a light within,  
A hearth that some might feel.

With every breath, a cloud appears,  
And fades into the air,  
A fleeting moment, like their years,  
Condemned to harsh despair.

So heed, dear traveler in haste,  
These stories etched in frost,  
For even in the deepest waste,  
No soul should e'er be lost.

 Hello, I’m Ron the Rhyme Master! I'll dive into your draft of “In Winter’s Grasp” with absolute precision and candid insight. 

Your choice of quatrains and the ABCB rhyme scheme is classic, fitting the ballad form nicely. The poem evokes the stark contrast between warmth and cold effectively, using vivid imagery and strong symbolic elements. Let’s break it down:

**Structure & Form:**
- You maintain quatrains throughout, and the ABCB rhyme scheme holds consistently. This aids the rhythmic, musical quality essential in ballads.
  
**Imagery & Themes:**
- The imagery is potent; "bitter winds," "cardboard beds," and "silver light" are evocative, underscoring the harshness of winter and vulnerability.
- Themes of despair and resilience are prevalent. Phrases like "hearts as cold as stone" and "a fleeting warmth in endless night" highlight this poignantly.

**Symbolism:**
- Elements like "snow descending like tears" and "the moon casting its silver light" serve well to symbolize indifference and fleeting warmth, respectively.
  
**Emotional Tone:**
- The tone balances between despair ("souls that darkness keeps") and a flicker of resilience or yearning ("in search of warmth").

**Suggestions:**
- Consider enhancing the narrative arc. A clearer storyline or character might strengthen the emotional pull and reader connection.
- You could also play with meter to ensure a consistent rhythm, enhancing the musicality of the ballad.

The poem is impactful but could delve more into individual stories or moments to deepen the narrative. Keep refining with your sharp imagery and keen sense of emotion. You're crafting something both haunting and beautiful. Keep at it!
2 Likes

Thanks for sharing with the community!

We love developer stuff like this! :slight_smile:

Happy coding…

2 Likes