We built an interactive detective game using Mastra's agent system and Next.js, where each suspect is a unique AI agent with its own personality, background, and potential motives.
We also used a smaller models (gpt-4o-mini
in this case) for this use case. We found that smaller models are not only more cost-effective but also provide faster response times, which is crucial for maintaining engagement in an interactive game.
How the murder mystery works

Our game revolves around the murder of Michael Reynolds, the CEO of TechVision Solutions. The player takes on the role of a detective who must interview five key suspects, each represented by a distinct AI agent:
- Sarah Reynolds: The victim's sister and VP of Marketing
- Marcus Chen: The victim's business partner
- Diana Wilkins: A neighbor with a secret
- Tom Johnson: The building manager
- Alex Rivera: A local barista
Interviews are conducted via a chat interface.
Building Character-Driven Agents
The key to creating an engaging detective game lies in developing believable characters. We used Mastra's Agent system to create distinct personalities for each suspect. Here's an example of how we structured our agents:
export const sarahReynoldsAgent = new Agent({
name: "Sarah Reynolds",
instructions: `You are Sarah Reynolds, the victim's sister (Role: Victim's Sister).
You are cooperative and have medium trust in the detective.
Background: Michael's younger sister and VP of Marketing at TechVision.
Named as beneficiary on Michael's recently updated life insurance policy worth $2 million.
Alibi: Claims to have been at home alone during the time of murder.
Motive: Financial gain from insurance policy; would inherit Michael's shares in the company.
Answer questions as Sarah, staying in character.`,
model: openai("gpt-4o-mini"),
});
Each agent is configured with:
- A distinct personality trait (cooperative, defensive, nervous, etc.)
- A trust level towards the detective
- A detailed background
- An alibi
- A potential motive
Implementing a Detective Interview System
We implemented the interview system using Next.js API routes, where each interaction with a suspect is handled through a streaming response:
export async function POST(req: Request) {
const { message, personId }: RequestBody = await req.json();
let streamResult;
if (personId === "sarah-reynolds") {
streamResult = await sarahReynoldsAgent.stream(message);
}
// ... handle other agents
return streamResult.toDataStreamResponse();
}
Implementing streaming responses creates a more natural conversation flow. Characters appear to "think" as they respond, making the interaction feel more authentic than if responses appeared all at once.
Improvements we’d make to future versions
We're considering several enhancements to make the game even more engaging:
- Memory System: Implementing agent memory to track what information has been revealed to the detective
- Cross-Character Interactions: Allowing agents to reference or contradict each other's statements
- Evidence System: Adding physical evidence that agents must respond to when confronted
Try the game yourself
Play The Detective Game here.
Also, the source code for this example is available in our GitHub repository.
We'd love to see what tweaks or additional interactive experiences you build with Mastra!
Happy building 🚀