Table of Contents
Introduction
Your AI agent cannot see live exchange rates unless you give it a tool.
LLMs can reason over training data, but they do not automatically know today’s EUR to GBP rate, the current price of a product, or whether a booking is available.
For that, the agent needs a controlled way to reach an external system. In practice, that means a function that calls an API and returns structured JSON.
This guide shows AI agents API tool use with ExchangeRatesData.
Key Takeaways
AI agents do not have live data by default. They need tools to access current external information.
Function calling lets a model request a predefined tool, pass arguments, receive JSON, and use the result in its response.
Exchange rates are a strong example because they change often and support fintech, ecommerce, travel, and invoicing workflows.
How do AI agents access real time data?
AI agents access real time data by calling tools.
A tool is a function your application exposes to the model. The model can request the tool when it needs external data, but your backend controls whether it runs, what it can access, and what it returns.
For currency conversion, the flow is simple. The user asks to convert 250 EUR to GBP. The model requests the tool. Your backend calls ExchangeRatesData and returns JSON for the final answer.
No stale rate. No hardcoded value. No guess.
What is tool use or function calling?
Tool use, also called function calling, lets an LLM ask your application to run code.
The model does not execute the API call itself. It returns a structured request. Your application validates it, calls the API, and sends the result back.
{
"name": "convert_currency",
"parameters": {
"type": "object",
"properties": {
"from_currency": {
"type": "string"
},
"to_currency": {
"type": "string"
},
"amount": {
"type": "number"
}
},
"required": ["from_currency", "to_currency", "amount"]
}
}
A prompt such as “Convert 250 EUR to GBP” can become this tool request:
{ "name": "convert_currency",
"arguments": {
"from_currency": "EUR",
"to_currency": "GBP",
"amount": 250
}
}
What the full tool-use loop looks like
Getting live data into an agent is a five-step round trip, not a one-way call:
The user asks something that needs current data (“Convert 250 EUR to GBP”).
You send the model the message plus your tool definitions. The model reads the tool descriptions and, if one fits, replies with a structured tool request instead of a normal answer. It does not run anything itself.
Your application validates that request and runs the matching function, which calls ExchangeRatesData.
You send the JSON result back to the model as a tool result.
The model reads the live data and writes the final answer for the user.
The model never touches your API key or the network. It only decides which tool to call and what to do with the result. And it only knows a tool exists from its name and description, so write those clearly. A vague description is why a model calls the wrong tool, or skips it.
Why exchange rates are a good agent example
Currency conversion is a useful LLM tool use example because the model should not answer from memory.
Exchange rates move. A rate that was valid yesterday may be wrong today. That matters for checkout pricing, travel budgets, invoices, and product localisation.
ExchangeRatesData includes endpoints for latest rates, conversion, historical rates, time series, and fluctuation data. This example uses /convert.
The /convert endpoint accepts amount, from, to, and optional date. The response includes success, query, info, date, and result.
How to call ExchangeRatesData from Python
Create an API key, then store it as an environment variable.
export APILAYER_API_KEY="your_api_key_here"
import os
import requests
API_KEY = os.environ["APILAYER_API_KEY"]
URL = "https://api.apilayer.com/exchangerates_data/convert"
def convert_currency(from_currency: str, to_currency: str, amount: float) -> dict:
response = requests.get(
URL,
headers={"apikey": API_KEY},
params={
"from": from_currency.upper(),
"to": to_currency.upper(),
"amount": amount
},
timeout=10
)
response.raise_for_status()
return response.json()
print(convert_currency("EUR", "GBP", 250))
This sends a request like this:
curl --request GET \
--url "https://api.apilayer.com/exchangerates_data/convert?from=EUR&to=GBP&amount=250" \
--header "apikey: your_api_key_here"
A successful /convert response follows this official structure:
{
"success": true,
"query": {
"from": "EUR",
"to": "GBP",
"amount": 250
},
"info": {
"timestamp": 1689953643,
"rate": 0.85642
},
"date": "2023-07-21",
"result": 214.105
}
The agent can now answer with the converted amount, rate, and date.
Wire the function to the model
The function above just calls the API. To make it an agent tool, you give the model a
description of it, let the model request it, run it, and hand the result back. This is
Anthropic’s Messages API; the OpenAI shape differs slightly (it nests the schema under
`parameters`), but the loop is the same.
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from your environment
# 1. Describe the tool so the model knows when to call it.
tools = [
{
"name": "convert_currency",
"description": "Convert an amount from one currency to another using live exchange rates.",
"input_schema": {
"type": "object",
"properties": {
"from_currency": {"type": "string", "description": "ISO code to convert from, e.g. EUR"},
"to_currency": {"type": "string", "description": "ISO code to convert to, e.g. GBP"},
"amount": {"type": "number", "description": "Amount to convert"},
},
"required": ["from_currency", "to_currency", "amount"],
},
}
]
messages = [{"role": "user", "content": "Convert 250 EUR to GBP."}]
# 2. The model sees the tools and decides to call one.
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=messages,
)
# 3. Run the tool the model asked for, then hand the result back.
for block in response.content:
if block.type == "tool_use":
result = convert_currency(**block.input) # the function from above
messages.append({"role": "assistant", "content": response.content})
messages.append({
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": block.id, "content": str(result)}
],
})
# 4. The model reads the live data and writes the final answer.
final = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=messages,
)
print(final.content[0].text)
# -> "250 EUR is about 214.11 GBP at today's rate of 0.85642."
Now the model never guesses a rate. It asks for the tool, your backend fetches the live
number, and the model answers from real data.
What makes an API good for agent tool use?
A good API for agent tool use is predictable.
Look for clear JSON fields, simple authentication, HTTPS, useful errors, and visible rate limits. ExchangeRatesData uses an API key in the apikey header and status codes such as 400, 401, 404, and 429.
Keep the key on your server. Do not expose it in prompts or browser code.
Start narrow. One tool such as convert_currency is enough. Validate currency codes, limit amounts, log calls, and handle rate limits before adding endpoints.
How to get started
Sign up for ExchangeRatesData. Get your API key. Store it as an environment variable. Call /convert from your backend. Return only the fields your agent needs.
The ExchangeRatesData API offers a free tier with 100 monthly requests, enough to prototype your agent’s financial tools today. Get your API key here.
Read the official ExchangeRatesData API documentation for parameters, authentication, and response formats.
For related reading, see APILayer’s posts on exposing APIs to LLMs securely, making REST APIs accessible to AI assistants with MCPs, and free currency converter APIs.
Explore APILayer Products
Discover APIs for geolocation, finance, aviation, weather, web scraping, AI, and more. Browse APILayer's product catalog to find the right APIs for your application and integrate the data and functionality your project needs.
Explore APILayer ProductsFind the right tools to start building.
FAQs
How do AI agents access real time data?
What is the difference between tool use and an API call?
Tool use is the model facing request. The API call is what your backend function performs after validation.
Should an LLM call ExchangeRatesData directly?
No. Keep the API key in your backend. The model should only see the tool schema and safe response data.
Which endpoint should I use for a currency agent?
Use /convert for one conversion. Use /latest for the latest available rates by base currency and symbols.
What should I do when the API returns a rate limit error?
Handle HTTP 429 in your backend, tell the user the limit has been reached, and avoid automatic retry loops.