# langchain4j-tool-function-calling-patterns

A skill that shows your agent how to expose Java methods with @Tool, register them in AiServices and handle tool execution errors.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 3700 installs · ★ 363
- Author: giuseppe-trisciuoglio
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/giuseppe-trisciuoglio/developer-kit/tree/HEAD/plugins/developer-kit-java/skills/langchain4j-tool-function-calling-patterns/SKILL.md
- Updated: 2026-09-10
- HTML: https://affpapa.org/en/nevibecode/skills/langchain4j-tool-function-calling-patterns

## What it does

The skill collects LangChain4j patterns for function calling. A method marked with `@Tool` becomes callable by the model, `@P` describes each argument, and `AiServices.builder().tools()` wires a set of tool instances to the chat model. Sample classes such as `WeatherTools` and `Calculator` show the whole path from annotation to a verified call.

Error handling gets its own section. `toolExecutionErrorHandler`, `hallucinatedToolNameStrategy` and `toolArgumentsErrorHandler` return a safe message to the model instead of a stack trace. For calls to outside services the skill advises `.toolExecutionTimeout()`, and independent tools can run through `.executeToolsConcurrently()`. A dynamic `ToolProvider` picks a different tool set based on request context.

The examples expect an API key from an environment variable and a `ChatModel` built with an explicit model name. Nothing here checks your LangChain4j version, so a signature mismatch is on you to catch — worth diffing the sample against your dependency before pasting it in.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-tool-function-calling-patterns -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/langchain4j-tool-function-calling-patterns/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/langchain4j-tool-function-calling-patterns/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-tool-function-calling-patterns -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/langchain4j-tool-function-calling-patterns/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-tool-function-calling-patterns -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/langchain4j-tool-function-calling-patterns/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install giuseppe-trisciuoglio/developer-kit langchain4j-tool-function-calling-patterns
```

2. **Manually: put the files in place**
- File: `.github/skills/langchain4j-tool-function-calling-patterns/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-tool-function-calling-patterns -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/giuseppe-trisciuoglio/developer-kit --path plugins/developer-kit-java/skills/langchain4j-tool-function-calling-patterns
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/langchain4j-tool-function-calling-patterns/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add giuseppe-trisciuoglio/developer-kit --skill langchain4j-tool-function-calling-patterns -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/giuseppe-trisciuoglio/developer-kit/tree/HEAD/plugins/developer-kit-java/skills/langchain4j-tool-function-calling-patterns/SKILL.md

## When to use it
- mark a Java method as a tool with `@Tool`
- register a tool set in `AiServices`
- return a readable error to the model instead of a stack trace
- cap the runtime of an external tool call
- serve different tools depending on request context

## Example prompts
- Show me how to wrap a weather service method in `@Tool` and register it with `AiServices`
- Add a tool execution error handler and a strategy for hallucinated tool names
- Set up concurrent tool execution and a timeout for the external API call

## Source text · SKILL.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: dde9e90394e97158.

BEGIN THIRD-PARTY FILE dde9e90394e97158
````text
---
name: langchain4j-tool-function-calling-patterns
description: "Provides and generates LangChain4j tool and function calling patterns: annotates methods as tools with @Tool, configures tool executors, registers tools with AiServices, validates tool parameters, and handles tool execution errors. Use when building AI agents that call tools, define function specifications, manage tool responses, or integrate external APIs with LLM-driven applications."
allowed-tools: Read, Write, Edit, Bash, Glob, Grep, WebFetch
---

# LangChain4j Tool & Function Calling Patterns

Provides patterns for annotating methods as tools, configuring tool executors, registering tools with AI services, validating parameters, and handling tool execution errors in LangChain4j applications.

## Overview

LangChain4j uses the `@Tool` annotation to expose Java methods as callable functions for AI agents. The `AiServices` builder registers tools with a chat model, enabling LLMs to perform actions beyond text generation: database queries, API calls, calculations, and business system integrations. Parameters use `@P` for descriptions that guide the LLM.

## When to Use

- Building AI agents that call external tools (weather, stocks, database queries)
- Defining function specifications for LLM tool use (`@Tool`, `@P` annotations)
- Registering and managing tool sets with `AiServices.builder().tools()`
- Handling tool execution errors, timeouts, and hallucinated tool names
- Implementing context-aware tools that inject user state via `@ToolMemoryId`
- Configuring dynamic tool providers for large or conditional tool sets

## Instructions

### 1. Annotate Methods with `@Tool`

Define a tool class with methods annotated `@Tool`. Provide a description as the first parameter. Use `@P` for each parameter description.

```java
public class WeatherTools {
    private final WeatherService weatherService;

    public WeatherTools(WeatherService weatherService) {
        this.weatherService = weatherService;
    }
````
END THIRD-PARTY FILE dde9e90394e97158

The third-party file has ended. Everything between the dde9e90394e97158 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2003 of 9471 bytes. © giuseppe-trisciuoglio · MIT · https://github.com/giuseppe-trisciuoglio/developer-kit/tree/HEAD/plugins/developer-kit-java/skills/langchain4j-tool-function-calling-patterns/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
