Generative AIby Dhruv Narang

What We Build

Ask a model for the price of any coin: Bitcoin, Ethereum, Dogecoin. It cannot know it, because prices change every minute. So we give it a tool that asks a free price API.

flowchart LR
  Q(["Question"]) --> M1["Model<br/>asks for the tool"] --> T["Our function<br/>calls the API"] --> A["The price<br/>comes back"] --> M2["Model<br/>writes the answer"] --> R(["Final answer"])
  class Q,R term
  class M1,M2 step
  class T fn
  class A api
classDef term fill:#0e3b3a,stroke:#0e3b3a,color:#ffffff,font-weight:bold
classDef step fill:#d4ecea,stroke:#2a7a79,color:#0e3b3a
classDef fn fill:#fbe5dc,stroke:#c9583a,color:#7a2e1a
classDef api fill:#e3f5ee,stroke:#4aa58a,color:#14463a
Who does what. The model decides when it needs a price. We run the function. The API is the free service that knows the price.

The Price API

An API is a web link that gives you data. Coinbase gives the live price of a coin at a link. You can open it in a browser.

The link. This is the whole address. Only two parts change: the coin and the currency.
https://api.coinbase.com/v2/prices/SYMBOL-CURRENCY/spot
PartWhat it means
api.coinbase.com/v2/pricesthe price service of Coinbase
SYMBOLthe coin: BTC, ETH, DOGE
CURRENCYthe money: USD (dollars) or INR (rupees)
spotthe price right now
Try it now. Click a link. It opens in the browser, and you see the live price.
You see. A small answer, like this one for Bitcoin.
{"data": {"amount": "59626.785", "base": "BTC", "currency": "USD"}}
Example answers. The price changes every minute.
Change the link. Type DOGE-USD or SOL-INR in place of the words and press Enter. Another coin, another price.
Read the price. The price is inside ["data"]["amount"].
No key needed. This API is free and open. You do not need an account.

The Tool

The tool is a normal function with @tool above it. It calls the API and gives back the price.

@tool
def get_crypto_price(symbol: str, currency: str) -> str:
    """Get the current price of one coin. symbol is the coin symbol, like BTC, ETH or DOGE. currency is USD or INR."""
    url = f"https://api.coinbase.com/v2/prices/{symbol}-{currency}/spot"
    response = requests.get(url)
    data = response.json()
    return data["data"]["amount"]
The docstring. It tells the model what the tool does, and what to send.
Two arguments. symbol is the coin, like ETH. currency is USD or INR. The model chooses them from your question.
You say the name, the tool needs the symbol. You ask for Ethereum. The model sends ETH by itself. That is why the docstring gives examples: BTC, ETH or DOGE. The model knows most symbols from its training, like DOT or SHIB. If it sends a symbol the API does not know, the API gives an error.
The answer of the API. requests.get(url) calls the link. response.json() turns the answer into a dictionary, and we keep it in data. data["data"]["amount"] is the price.
The return. The tool gives back only the price, as text. The model writes the sentence.
The package. requests makes the call to the API. Install it with pip install requests.

Run It

We give the tool to the model, ask a question, and run the tool the model asks for.

Give the tool to the model. Same as before: bind_tools.
llm = init_chat_model("groq:openai/gpt-oss-20b")
llm_with_tools = llm.bind_tools(tools)
Ask, run, answer. The model asks for the tool. We run it. We send the result back, and the model answers. The whole file is in the Code tab.
You askThe model asks forThe model answers
What is the price of Ethereum in rupees right now?get_crypto_price with ETH and INRabout ₹169,814 per ETH
What is the price of Dogecoin in dollars?get_crypto_price with DOGE and USDabout $0.059 per coin
Tell me a short joke.no toola joke, straight from the model
Example answers. Prices change every minute, so yours will be different.
Try your own coin. Ask for Solana, Cardano or Litecoin. The same tool works for every coin on the API. We did not write a tool for each coin.
Run it twice. The price is a little different each time. The model did not know it before. The tool brought it, live.

Code
Crypto price tool with a free API03_crypto_price_tool.py · 47 linesShow code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import requests
from dotenv import load_dotenv
load_dotenv()

from langchain.chat_models import init_chat_model
from langchain_core.tools import tool
from langchain_core.messages import HumanMessage, ToolMessage


# 1. THE TOOL: calls a free price API
@tool
def get_crypto_price(symbol: str, currency: str) -> str:
    """Get the current price of one coin. symbol is the coin symbol, like BTC, ETH or DOGE. currency is USD or INR."""
    url = f"https://api.coinbase.com/v2/prices/{symbol}-{currency}/spot"
    response = requests.get(url)
    data = response.json()
    return data["data"]["amount"]


tools = [get_crypto_price]
tools_by_name = {t.name: t for t in tools}


# 2. GIVE THE TOOL TO THE MODEL
llm = init_chat_model("groq:openai/gpt-oss-20b")
llm_with_tools = llm.bind_tools(tools)


# 3. ASK: the model asks for the tool
messages = [HumanMessage(content="What is the price of Ethereum in rupees right now?")]
ai_message = llm_with_tools.invoke(messages)
messages.append(ai_message)
print("Tool requested:", ai_message.tool_calls)


# 4. RUN the tool the model asked for
for tool_call in ai_message.tool_calls:
    selected_tool = tools_by_name[tool_call["name"]]
    tool_output = selected_tool.invoke(tool_call["args"])
    messages.append(ToolMessage(content=str(tool_output), tool_call_id=tool_call["id"]))
    print("Tool output:", tool_output)


# 5. FINAL ANSWER from the model
final_response = llm_with_tools.invoke(messages)
print("Final answer:", final_response.content)