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.
Defining a tool
Section titled “Defining a tool”@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" ); });Using tools in generation
Section titled “Using tools in generation”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."How it works
Section titled “How it works”- You define tools with a name, description, and input schema
- The model receives the tool definitions alongside your prompt
- If the model decides to call a tool, Genkit automatically executes it
- The tool result is sent back to the model for final response generation
Typed tools
Section titled “Typed tools”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);Next steps
Section titled “Next steps” DotPrompt Manage prompts as template files
Multi-Agent Build multi-agent orchestration
MCP Plugin Connect to MCP tool servers