Tools
AI models (LLMs) today can only respond with text - they can’t look up live information or take actions - so apps built on them can’t answer questions like “What’s my calendar today?” or “Book me a table nearby.”
Tool calling solves this by letting the AI use real software functions that can fetch data or perform tasks - like checking the weather, reading your calendar, or sending a message (these are called tools). This leads to more accurate answers, faster responses, and apps that actually do things for you.
Example:
from litai import LLM, tool
@tool
def get_weather(location: str):
return f"The weather in {location} is sunny"
llm = LLM(model="openai/gpt-4")
response = llm.chat("What's the weather in Tokyo?", tools=[get_weather])
result = llm.call_tool(response, tools=[get_weather])
# The weather in London is sunny
result = llm.chat("What's the weather in London?", tools=[get_weather], auto_call_tools=True)
# The weather in London is sunny
Simple tools
The @tool decorator is useful when you just need a quick, simple tool. It lets you turn a regular function into something the model can use, without any extra setup. It's perfect for tools that don't need to remember anything between uses (ie: stateless), making it easy to test ideas and build fast - especially for small, one-off tasks.
from litai import LLM, tool
@tool
def get_weather(location: str):
return f"The weather in {location} is sunny"
llm = LLM(model="openai/gpt-4")
response = llm.chat("What's the weather in Tokyo?", tools=[get_weather])
result = llm.call_tool(response, tools=[get_weather])
# The weather in London is sunny
Advanced tools
To build more advanced tools that do more than one quick task, use the LitTool class. It’s a good fit when your tool needs setup ahead of time - like connecting to a service (such as a database) or keeping information between uses. It gives you more structure and flexibility for solving complex problems, and makes it easy to share tools with others.
For example, here's a tool that loads knowledge to help you answer a question.
from litai import LLM, LitTool
class FAQTool(LitTool):
def __init__(self):
self.faq = {
"pricing": "You can view our pricing plans on the website.",
"support": "Our support team is available 24/7 via chat.",
"refund": "Refunds are available within 30 days of purchase."
}
def run(self, question: str) -> str:
keyword = question.lower()
for topic, answer in self.faq.items():
if topic in keyword:
return answer
return "Sorry, I couldn't find an answer for that."
tool = FAQTool()
llm = LLM(model="openai/gpt-4")
response = llm.chat("How do I get a refund?", tools=[tool])
result = llm.call_tool(response, tools=[tool])
print(result) # → "Refunds are available within 30 days of purchase."
Reuse other tools
If you have built tools in other agent frameworks (Langchain, etc...) you can use those with LitAI. Under the hood, the LitTool is a pydantic BaseModel.
LitAI offers native support for LangChain tools. The following example demonstrates how to integrate and use LangChain within LitAI.
from langchain_core.tools import tool as langchain_tool
from litai import LLM
@langchain_tool
def get_weather(city: str) -> str:
"""Get the weather of a given city"""
return f"Weather in {city} is sunny."
llm = LLM("openai/gpt-4")
llm.chat("What's the weather in Tokyo?", tools=[get_weather])
How to choose
Use @tool when you need a quick, lightweight tool - like for prototyping or small tasks that don’t need to store data or connect to other services. It keeps things simple and fast to build.
As a tool grows - if it needs to connect to a database, keep information between calls, or run setup code - switch to a LitTool class. It gives you more structure and flexibility to handle advanced logic and makes it easier to reuse or share the tool with others.
| Feature | @tool (decorator) | class LitTool |
|---|---|---|
| Ease of use | ✅ Simpler, more concise | ❌ More boilerplate |
| Good for quick demos | ✅ Yes | ❌ Verbose |
| Supports internal state | ❌ No | ✅ Yes |
| Custom setup/teardown | ❌ Hard | ✅ Easy via methods |
| Code organization | ❌ Flat | ✅ Encapsulated |
| Advanced logic | ❌ Hard to scale | ✅ Easy to grow into |
Was this page helpful?
Thanks for the feedback.