Skip to main content

Overview

The tool use workflow follows four steps:
  1. Define tools as JSON in the system prompt or via .apply_chat_template()
  2. Generate a response where the model outputs function calls between special tokens
  3. Execute the function and return results as a tool role message
  4. Regenerate to let the model interpret results and respond to the user
LFM2.5 wraps tool calls in <|tool_call_start|> and <|tool_call_end|> tokens. LFM2 additionally wraps tool definitions in <|tool_list_start|>/<|tool_list_end|> and responses in <|tool_response_start|>/<|tool_response_end|>. By default, models generate Pythonic function calls. Add โ€œOutput function calls as JSONโ€ to your system prompt for JSON format.

Defining Tools

You have two options for providing tool definitions. Option 1: JSON in the system prompt (recommended)
Option 2: Python functions via .apply_chat_template()

Complete Example

Start by loading the model and defining your tools and conversation:
Generate the first response. The model may decide to call a tool:
The model outputs a tool call wrapped in special tokens:
Parse the tool call and execute the function externally. Here we use a mock function that simulates what would typically be an API call to your recruitment system:
Generate again so the model can interpret the tool result and respond to the user:
The full conversation now looks like this:

Vision Models

LFM2-VL and LFM2.5-VL vision models support text-only function calling. Use the tokenizer from the processor:

Best Practices

Tool definitions consume context tokens since theyโ€™re inserted as text. For large tool lists (100+ tools), this can use significant portions of your context window. To manage this effectively, only include tools relevant to the current request. Write clear, concise descriptions. Group related tools and remove unused ones when theyโ€™re not needed.