DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Crash-Proof Agents: Durable Execution for Long-Running AI Systems
  • Beyond State Consistency: Preventing Stale Decisions in AI Agent Systems
  • Beyond Manual Mapping: AI-Orchestrated Governance for Hybrid Cloud
  • Applying Microservices Lessons to Multi-Agent AI Systems

Trending

  • A/B Testing Under Shared Constraints: Supply, Inventory, and User Attention
  • Understanding Vector Databases: Concepts, Similarity Search, and Trade-offs for Enterprise Applications
  • Why Your CI Pipeline Is Flaky and How to Make It Reliable
  • Your Agents Think Data Federation Means One Truth. It Means a Collage.

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Where the Model Stops and the Code Starts: Lessons From an AI Procurement Agent

Where the Model Stops and the Code Starts: Lessons From an AI Procurement Agent

Two agent buyer and seller: price rules belong in code rather than prompts, and human approval only means something if the commitment itself is gated.

By 
TAPASH BISHOI
·
Nov. 23, 26 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
48 Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Most agent demos stop at one agent calling a few tools. Procurement is a harder test. It involves state, money, a counterparty with its own rules, and a human who has to approve anything binding. So I built two systems that talk to each other: a buyer agent that negotiates on behalf of a purchasing user, and a seller platform that runs its own AI negotiator behind a catalog, inventory, and fulfillment API.

This article covers how the pieces fit, what worked, and where I'd do things differently.

The Shape of the System

There are two repositories. The buyer side (tapashbishoi/buyerproject) is a Node.js and TypeScript app built on Google's Agent Development Kit (ADK), with Gemini as the reasoning model. It serves a chat UI over Server-Sent Events so the user can watch the agent work. The seller side (tapashbishoi/SellerProject) is an Express service backed by PostgreSQL and RabbitMQ. It exposes a REST API, an MCP endpoint for AI clients, an EDI interface for traditional trading partners, and a negotiation evaluator that calls Gemini.

I should be precise about "agent-to-agent," because this isn't the Google A2A specification. The buyer is an LLM that reasons over a set of tools. The seller serves those tools over MCP. One of them, negotiate_price, calls a second LLM on the seller side to evaluate the offer. So the protocol between the agents is MCP tool calls, and the second agent sits behind a tool boundary. That's a useful way to think about it: the buyer sees the seller's outcomes, not its pricing rules. 

Wiring the Buyer Agent to the Seller's Tools

The connection on the buyer side is a few lines. ADK's MCPToolset discovers the seller's tools at runtime over streamable HTTP, so the agent doesn't need a hand-written client for each one:

TypeScript-JSX
 
const mcp = new MCPToolset({
  type: 'StreamableHTTPConnectionParams',
  url: SELLER_MCP_URL
});

this.agent = new LlmAgent({
  name: 'BuyerAgent',
  model,
  instruction: SMART_ORDERING_PROMPT,
  tools: [mcp]
});


The buyer doesn't hardcode the seller's API. If the seller adds a tool, the agent can use it after reconnecting, provided the instruction mentions it. The cost is that the prompt becomes the orchestration layer. Most of the buyer's behavior lives in one long instruction that sets the order of operations: check registration, register if needed, browse, check delivery, negotiate, then present a proposal. The model follows that sequence well most of the time. "Most of the time" is the honest answer, and it's why the instruction carries so much weight.

The agent also has to produce something a human can approve. The prompt asks for a JSON block wrapped in PROPOSAL_START and PROPOSAL_END tags, containing line items, shipping, delivery days, total, and a short reasoning field. The server extracts that block with a regex, parses it, and sends it to the browser as a separate event so the UI can render an approval card. Regex extraction from model output is brittle. For production, I'd switch to a structured tool call. For a prototype, it's easy to read and debug.

What the Seller Does When an Offer Arrives

The seller's negotiation path is where most of the logic lives. An incoming offer goes through negotiate_price, which looks up the product, checks stock, and computes two thresholds from the list price: a 70% floor, the lowest price the seller will counter at, and a 60% hard reject, below which it won't negotiate. 

The hard reject is enforced in code:

TypeScript-JSX
 
if (buyer_offer < hard_reject_price) {
  // Persist a rejected negotiation row and return immediately.
  // No model call, no counter-offer.
}


Anything above that threshold goes to Gemini. The evaluator gets the product, quantity, buyer history, shipping cost to the buyer's region, round number, and previous counter. It returns a JSON decision of accept, counter, or reject, with a counter price, a message for the buyer, and internal reasoning.

The prompt encodes commercial policy in plain English. Loyal buyers, meaning three or more orders or $500 spent, can get a little extra. Large orders can go closer to the floor. Buyers far from a distribution center, where shipping eats more than 15% of the order value, get less flexibility. By round three, the seller calls its offer final. This is a reasonable way to express judgment that's hard to write as conditionals.

But look at what the code does with the model's output. It stores ai.counter_price directly. It never compares that value to floor_price. The floor exists only in the prompt, as text the model is asked to respect. Models usually follow instructions like that, but that's not a pricing control. A counter below the floor is a real business risk, and the fix is one line: 

TypeScript-JSX
 
const counter_price = Math.max(ai.counter_price ?? 0, floor_price);


This is the first lesson: put hard constraints in code and use the model for judgment within them. The buyer repo's README already lists a similar guardrail as future work, a BeforeToolCallback that rejects out-of-policy calls. The seller needs the same kind of enforcement. 

The Approval Gate That Isn't Quite a Gate

The second lesson is about human-in-the-loop design, and it's more subtle.

The buyer's instruction says the agent must present a proposal and wait for approval before calling accept_counter_offer or place_order. That's the intent. But when the seller's evaluator returns accept on the first offer, handleNegotiation inserts an order with status queued and publishes it to RabbitMQ before returning anything. The buyer agent doesn't have to call anything to place that order. It's already placed.

Picture a buyer who asks for a bulk discount, gets an immediate acceptance, and then the chat model pauses to ask for confirmation. The order is already queued. The human thought they were reviewing a proposal, but they had already committed to a purchase. The approval step is a UI convention on the buyer side, and the commitment happens on the seller side, and nothing connects the two.

The fix is architectural. An offer the seller would accept should return a binding quote with an expiry. The order should be created only when the buyer calls an explicit commit tool with that quote's ID. That separates "what would the seller do" from "do it." Stateless quoting with a short TTL is worth borrowing from any system where side effects are expensive to reverse.

Routing Orders to the Nearest Distribution Center

The seller maps each US state to one of three zones, East, Central, or West, and each zone has a distribution center. When an order comes in, the router picks the fulfilling DC, estimates shipping days from zone distance, and computes a cost with a volume multiplier. The routing result also goes to the negotiator, so the model can see that a buyer in Montana is far from the nearest warehouse and factor that in.

I kept the zone model simple on purpose. A real carrier integration would use distance matrices and rate cards. Three zones is enough to show the flow and keep the numbers easy to reason about when you're debugging a negotiation. Start simple, and make the simplification visible in the code so nobody mistakes it for the real thing later.

Getting Orders Out of the Request Path

Orders aren't written to the database synchronously on the request. The seller publishes them to a RabbitMQ queue as persistent messages. A separate consumer picks them up, locks the inventory rows with SELECT ... FOR UPDATE, validates stock, inserts line items, deducts inventory from both the DC and the global count, and marks the order confirmed or failed.

The queue separates two concerns with different failure modes and latencies. The MCP server can answer in milliseconds with "order accepted, queued," and the consumer can take its time. The tradeoff is that the buyer gets a provisional answer, and the order can still fail at consumption if stock ran out in the meantime. The system records a failure reason on the order, which the buyer can read through track_order. The design is sound, but the user experience has a gap: a buyer told "queued" needs a clear way to learn it failed. The tool descriptions spell out the status states, and the model needs to be told how to explain them.

What I'd Tell Someone Building This

Use tools as the boundary between agents, and treat that boundary as an API contract. The seller doesn't need to expose its pricing policy or prompt to the buyer. It exposes outcomes, and that separation is the point of the design.

Put business constraints in code, not prompts. Prompts are good at judgment and bad at invariants. Price floors, quantity limits, and approval requirements should be checked by ordinary code the model can't talk its way around.

Make commitment explicit. Anything with side effects that cost money or are hard to reverse should require a separate, deliberate call, ideally against a quote the human has seen. An agent that can place an order in the same call that prices it will eventually place one the human didn't mean to approve.

Keep the human in the loop at the level of the commitment, not the chat. The chat UI is a convenience. The approval has to be enforced by the system that executes the purchase.

Build observability early. The buyer's SSE stream, which sends tool calls, tool results, and thoughts to the browser, was the most useful debugging tool in the project. You can watch the agent decide, see which tool it called and what came back, and catch the moment it goes off script. Agent systems fail in sequences rather than single steps, so you need the sequence visible.

Two agents talking over MCP is a more natural fit than it looks. The hard parts aren't the protocol. They're deciding which rules belong to the model and which belong to code, and making sure a human's "yes" means what they think it means.

AI systems

Opinions expressed by DZone contributors are their own.

Related

  • Crash-Proof Agents: Durable Execution for Long-Running AI Systems
  • Beyond State Consistency: Preventing Stale Decisions in AI Agent Systems
  • Beyond Manual Mapping: AI-Orchestrated Governance for Hybrid Cloud
  • Applying Microservices Lessons to Multi-Agent AI Systems

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook