3 min read

Currency Conversion (Determinism)

A simple tool that proves a big point: Why we need 'Islands of Truth' in a sea of hallucination.

Dw

Dwizi Team

Editorial

Currency Conversion (Determinism)

This might seem like a boring use case. "Converting USD to EUR? Who cares? I can Google that."

But this seemingly trivial example illustrates the single most important concept in AI engineering: Determinism.

If you ask an LLM: "How much is 100 USD in EUR?"

It might say: "As of my last update in 2023, it's about 92 Euros." Or, if the temperature is high, it might hallucinate: "It is exactly 85 Euros."

Neither answer is acceptable if you are running an e-commerce store. You need the rate right now, and you need it to be exact. You are dealing with money. "Probably right" means "Definitely sued."

The Problem: Static Knowledge vs. Dynamic Reality

LLMs are frozen in time. They know the past perfectly, but they are blind to the present.

Furthermore, they are probabilistic. They predict the next word. But math isn't about predicting the next digit; it's about calculating the value. 2 + 2 is not "probably 4"; it is "definitionally 4."

Any question that involves a changing number (price, weather, time, exchange rate) or a rigid calculation is a weakness for an LLM.

The Solution: A Source of Truth

We need to inject an Island of Truth into the conversation.

We build a convert_currency tool. This tool wraps a reliable, external exchange rate API. It doesn't guess. It looks up the fact.

The Implementation

/**
 * Converts an amount from one currency to another using live rates.
 * 
 * Description for LLM: "Use this to convert currency. Do not guess the rate."
 */

type Input = {
  amount: number;
  from: string; // "USD"
  to: string;   // "EUR"
};

export default async function convertCurrency({ amount, from, to }: Input) {
  // 1. The Source
  // We use a deterministic external API.
  // The API guarantees that if we ask for the rate at 12:00:00, 
  // everyone gets the same number.
  const url = `https://api.frankfurter.app/latest?amount=${amount}&from=${from}&to=${to}`;
  
  const res = await fetch(url);
  
  if (!res.ok) {
     return { error: "Currency not supported or API down." };
  }

  const data = await res.json();

  // The API returns { rates: { EUR: 92.5 } }
  const result = data.rates[to];

  // 2. The Deterministic Return
  // We return the exact float.
  // We also return the rate used, for transparency.
  return {
    original: amount,
    converted: result,
    rate: result / amount,
    timestamp: data.date
  };
}

The Execution Story

User: "I want to buy this bag for $150 USD. How much is that in Euros?"

Agent Action:

  1. Analyze: The Agent recognizes a financial query. It knows it cannot trust its own internal weights for this.
  2. Call: It calls convert_currency({ amount: 150, from: "USD", to: "EUR" }).
  3. Execute: The tool hits the API. The API says the rate is 0.92. The result is 138.0.
  4. Return: The tool returns { converted: 138.0 }.
  5. Synthesize: The Agent takes this fact and wraps it in natural language.
    • "That would be €138.00. The current exchange rate is 0.92."

The Philosophy

The LLM is great at the "soft" parts of the interaction: being polite, understanding the user's intent ("buy this bag"), and formatting the answer.

The Tool is great at the "hard" part: the number.

By combining them, you get the best of both worlds: The empathy of a human and the precision of a calculator. This hybrid architecture is the only way to build reliable AI applications.

Subscribe to Dwizi Blog

Get stories on the future of work, autonomous agents, and the infrastructure that powers them. No fluff.

We respect your inbox. Unsubscribe at any time.

Read Next

The Junior Dev (GitHub)

Automating the first 15 minutes of every bug report. How to build an agent that triages issues.

Read article