Tool use with Llama API (and reasoning)
Meta's released a new API for their Llama models.
We'll show you how to get improved tool-calling / function-calling with BAML (our prompting framework) (github). BAML lets you write prompts for structured extraction using a simple syntax.
We'll additionally show how to make the default Llama models do tool-calling / function-calling WITH reasoning.
Solving a reasoning problem with a schema
Imagine we are trying to figure out the employee hierarchy chart for a company with this:
George is the CEO of the company. Kelly is the VP of Sales. Asif is the global head of product development. Mohammed manages the shopping cart experience. Tim manages sales in South. Stefan is responsible for sales in the f100 company. Carol is in charge of user experience"
Here is an interactive example that you can run to see how Llama 4 can solve this reasoning problem!
You can run this in python like this:
from baml_client import b
response = b.ExtractHierarchy(message="""
George is the CEO of the company.
Kelly is the VP of Sales. Asif is the global head of product development.
Mohammed manages the shopping cart experience.
Tim manages sales in South.
Stefan is responsible for sales in the f100 company.
Carol is in charge of user experience""")
print(response) # fully type-safe and validated!
Tool calling with Llama 4 and BAML
In BAML you can also use several tools. Here's another example
You can call this in python like this (we also support other languages!):
from baml_client import b
from baml_client.types import ProductSearch, ScheduleAppointment
response = b.ChooseOneTool(user_message="Find me running shoes under $100 in the sports category")
print(response)
if isinstance(response, ItemSearch):
print(f"Item Search called:")
print(f"Query: {response.query}")
print(f"Max Price: ${response.maxPrice}")
print(f"Category: {response.category}")
elif isinstance(response, BookAppointment):
print(f"Book Appointment called:")
print(f"Customer: {response.clientName}")
print(f"Service: {response.serviceRequested}")
print(f"Date: {response.datePreferred}")
print(f"Duration: {response.timeDuration} minutes")
Tool calling with Llama 4 and BAML with reasoning
In BAML you can also use several tools. Here's another example
The calling code has no changes! BAML will automatically pull out only the tool call part of the API response. Try pressing play.
from baml_client import b
from baml_client.types import ProductSearch, ScheduleAppointment
response = b.ChooseOneTool(user_message="Find me running shoes under $100 in the sports category")
print(response)
if isinstance(response, ItemSearch):
print(f"Item Search called:")
print(f"Query: {response.query}")
print(f"Max Price: ${response.maxPrice}")
print(f"Category: {response.category}")
elif isinstance(response, BookAppointment):
print(f"Book Appointment called:")
print(f"Customer: {response.clientName}")
print(f"Service: {response.serviceRequested}")
print(f"Date: {response.datePreferred}")
print(f"Duration: {response.timeDuration} minutes")
BAML is fully open-source and free to use, and it works with many other languages (Ruby, TS, Python, etc).
Check out the docs for more!