DEV Community

Cover image for AI Agents Negotiating the Road: Building PathPact
Harish Kotra (he/him)
Harish Kotra (he/him)

Posted on

AI Agents Negotiating the Road: Building PathPact

Route selection is a complex problem. For autonomous vehicles, it's not just about the fastest path; it's about balancing speed, safety, and efficiency. But what happens when multiple vehicles with different personalities compete for the same road?

In this blog post, I'll walk you through how I built PathPact, a multi-agent simulation that uses Google Gemini AI to resolve route conflicts between autonomous agents.

The Vision: Multi-Agent Negotiation

Most routing apps give you the "best" route. But "best" is subjective. A Speedster wants the fastest time, regardless of tolls. A Scenic Cruiser wants to avoid highways and see the sights. An Efficiency Expert wants the shortest distance to save fuel.

When these agents are assigned to the same road network, conflicts arise. My goal was to create a simulation where these agents could "talk" to each other and negotiate their assignments based on their unique personalities.

The Architecture: React, Leaflet, and Gemini

The app is built with a modern stack that prioritizes performance and visual clarity:

  • React 18 & Vite: For a lightning-fast development experience and a reactive UI.
  • Leaflet & OpenStreetMap: To handle the heavy lifting of geospatial rendering.
  • Google Gemini 1.5 Flash: The "brain" of the simulation, handling the complex reasoning required for negotiation.

1. Fetching the Routes (OSRM)

Before the agents can negotiate, we need real-world data. I used the Open Source Routing Machine (OSRM) to fetch actual road-network paths between two points.

const coords = waypoints.map(w => `${w.lng},${w.lat}`).join(';');
const url = `https://router.project-osrm.org/route/v1/driving/${coords}?overview=full&alternatives=true&geometries=geojson`;
const response = await fetch(url);
const data = await response.json();
Enter fullscreen mode Exit fullscreen mode

2. The Negotiation Protocol (Gemini AI)

This is where the magic happens. I pass the agent profiles and the fetched route data (including traffic and weather) to Gemini 1.5 Flash. The prompt is carefully structured to ensure the AI stays in character and provides a valid JSON response.

const systemInstruction = `
  You are a traffic coordinator for three autonomous agents.
  Each agent has a distinct personality and preferences.
  Your task is to assign exactly one route per agent.
  Provide a transcript of their negotiation and the final assignments.
`;
Enter fullscreen mode Exit fullscreen mode

Gemini's reasoning capabilities allow it to understand that the Efficiency Expert might yield a faster route to the Speedster if the alternative is safer or shorter, even if it's slightly slower.

3. Visualizing the Conflict (Leaflet & Offset Paths)

One of the biggest challenges was visualizing overlapping routes. If all three agents are on the same road, their lines would overlap, making it impossible to see who is where.

I solved this by implementing a path offset algorithm:

const agentIndex = AGENTS.findIndex(a => a.id === agent.id);
const offset = (agentIndex - 1) * 0.00008; // Roughly 8-10 meters separation
const offsetPath = path.map(([lat, lng]) => [lat + offset, lng + offset]);
Enter fullscreen mode Exit fullscreen mode

This ensures that even on the same road, each agent has its own distinct lane, making the simulation clear and easy to follow.

Key Technical Challenges

  • Geocoding & Routing: Balancing the limitations of free APIs (like Nominatim) with the need for accurate data.
  • Real-time Animation: Ensuring the agent icons follow the offset paths smoothly during the simulation.
  • AI Prompt Engineering: Fine-tuning the prompt to get consistent, high-quality negotiation transcripts that feel "human" and stay true to the agent's personalities.

Building Route Negotiator was a fascinating exploration of how LLMs can be used for multi-agent reasoning in a geospatial context. It's not just a map; it's a window into how autonomous systems might one day communicate and cooperate on our roads.

Checkout Github Repo: https://github.com/harishkotra/pathpact

Follow more builds at dailybuild.xyz

Top comments (1)

Collapse
 
jokoprecious222 profile image
Precious olivier

Good Morning
Sorry if we approached you wrongly, our names are Precious and Olivier
Johnson Joko . l am 17 years old.
We would love to discuss a business proposal in private with you please reply me to our email address so i can explain more to you

renatojames04@yahoo.com
Kind Regards
Precious and Olivier