Skip to content

Tool Calling

Tools extend the capabilities of AI models by giving them the ability to call your functions. Models trained for tool calling can respond with a request to invoke a tool, and Genkit handles the execution automatically.

@SuppressWarnings("unchecked")
Tool<Map<String, Object>, Map<String, Object>> weatherTool = genkit.defineTool(
"getWeather",
"Gets the current weather for a location",
Map.of(
"type", "object",
"properties", Map.of(
"location", Map.of(
"type", "string",
"description", "The city name"
)
),
"required", new String[]{"location"}
),
(Class<Map<String, Object>>) (Class<?>) Map.class,
(ctx, input) -> {
String location = (String) input.get("location");
return Map.of(
"location", location,
"temperature", "72°F",
"conditions", "sunny"
);
});

Pass tools to generate() — the tool execution loop is handled automatically:

ModelResponse response = genkit.generate(
GenerateOptions.builder()
.model("openai/gpt-4o")
.prompt("What's the weather in Paris?")
.tools(List.of(weatherTool))
.build());
System.out.println(response.getText());
// Output: "The weather in Paris is currently 72°F and sunny."
  1. You define tools with a name, description, and input schema
  2. The model receives the tool definitions alongside your prompt
  3. If the model decides to call a tool, Genkit automatically executes it
  4. The tool result is sent back to the model for final response generation

You can also define tools with typed input/output classes:

Tool<RecipeRequest, MenuItem> recipeGen = genkit.defineTool(
"generateRecipe",
"Generates a recipe",
(ctx, request) -> new MenuItem(...),
RecipeRequest.class,
MenuItem.class
);