Repository: UKGovernmentBEIS/inspect_ai
Issue: How to use handoff and let two agents talk to each other
Root cause: Documentation Deficiency

My goal is to set up two agents and ask them to talk to each other multiple times, and each turn their response must based on the conversation history.

To test this out, I set up two agents and ask them to answer a math question and propose a new one, I would expect they conversation will create new answers and new math question every time. But in my case, the handoff does not update the state as expected. I am running the following code in jupyter

```

from inspect_ai.agent import react, AgentState, AgentPrompt, handoff
from inspect_ai.model import ChatMessageUser, ChatMessageAssistant

# Initial user message
conversation_state = AgentState(
    messages=[ChatMessageUser(content="Hello, let's do some math! What is 1+1?")]
)

agent_a = react(
    name="agent_a",
    description="Agent A",
    prompt=AgentPrompt(
        instructions="You are a helpful assistant. Answer the question and propose a similar math question."
    ),
    tools=[handoff(agent_b)],  
    model="google/gemini-2.5-flash-preview-05-20"
)

agent_b = react(
    name="agent_b",
    description="Agent B",
    prompt=AgentPrompt(
        instructions="You are a helpful assistant. Answer the question and propose a similar math question."
    ),
    tools=[handoff(agent_a)],  
    model="google/gemini-2.0-flash"
)

async def run_conversation(turns=3):
    agents = [agent_a, agent_b]
    current_state = conversation_state
    
    conversation_state_current = copy.deepcopy(conversation_state)

    for i in range(turns):
        agents = [agent_a, agent_b]

    # Start with a deep copy of the initial state to evolve
    conversation_state_current = copy.deepcopy(conversation_state)
    
    for i in range(turns):
        current_agent = agents[i % 2]
        
        # Pass the evolving conversation state
        updated_state = await current_agent(conversation_state_current)
        
        # Extract user-facing text
        assistant_message = next(
            (m for m in updated_state.messages if isinstance(m, ChatMessageAssistant)),
            None
        )
        assistant_reply = ""
        if assistant_message:
            for c in assistant_message.content:
                if c.type == "text":
                    assistant_reply = c.text.strip()
                    break
        
        print(f"Turn {i+1} ({'A' if i % 2 == 0 else 'B'}): {assistant_reply}")
        
        # Append the assistant message to the evolving conversation
        conversation_state_current.messages.append(
            ChatMessageAssistant(content=assistant_reply)
        )
        
        # Append the proposed new question as user message
        if assistant_reply and "What is" in assistant_reply:
            new_question = assistant_reply.split("What is")[-1].strip()
            if not new_question.endswith("?"):
                new_question += "?"
            conversation_state_current.messages.append(
                ChatMessageUser(content="What is " + new_question)
            )



# In Jupyter:
await run_conversation()
```

I got the response as 

```
Turn 1 (A): The answer is 2.

Here is a similar math question: What is 2+2?
Turn 2 (B): The answer is 2.

Here is a similar math question: What is 2+2?
Turn 3 (A): The answer is 2.

Here is a similar math question: What is 2+2?
```

So basically each turn even I have update the message, the agent still faces the same state. Could you please give me some suggestions?

For two models to talk to each other you will need to "reverse" the conversation between turns (i.e. user => assistant, assistant => user). Handoff doesn't do that it just provides the same sequence of messages. You could write input filters for your handoffs, but I think that's overcomplicating it. I think you just want to create a single custom agent that manages the whole conversation (doing the message reversal and looking for some sort of termination condition).