Skip to main content
An Agent Strategy Plugin gives an LLM the reasoning and decision-making logic to choose tools, call them, and handle their results, so it can solve problems autonomously. This guide walks through building a Function Calling strategy that lets the model fetch the current time on its own.

Prerequisites

  • Dify plugin scaffolding tool
  • Python environment (version 3.12)
For details on preparing the plugin development tool, see CLI.
Run dify version in your terminal to confirm that the scaffolding tool is installed.

1. Initialize the Plugin Template

Run the following command to create a development template for your Agent plugin:
Follow the on-screen prompts; the comments below explain each choice.
Initialization creates a folder with everything you need for plugin development:
All key functionality for this plugin is in the strategies/ directory.

2. Develop the Plugin

Agent Strategy Plugin development revolves around two files:
  • Plugin Declaration: strategies/basic_agent.yaml
  • Plugin Implementation: strategies/basic_agent.py

2.1 Define Parameters

Start by declaring the plugin’s parameters in strategies/basic_agent.yaml. These parameters power the plugin’s core features, such as calling an LLM or using tools. We recommend starting with these four parameters:
  • model: The large language model to call (e.g., GPT-4, GPT-4o-mini).
  • tools: A list of tools that enhance your plugin’s functionality.
  • query: The user input or prompt content sent to the model.
  • maximum_iterations: The maximum iteration count, which prevents excessive computation.
Example:
Dify automatically renders a configuration interface from these parameter declarations:
Agent Strategy Plugin UI

2.2 Retrieve Parameters and Execute

When users fill out these fields, your plugin receives the submitted values. In strategies/basic_agent.py, define a Pydantic model that validates the incoming parameters:
Then parse the parameters in _invoke and run your strategy logic:

3. Invoke the Model

Invoking the model is central to an Agent strategy. Use session.model.llm.invoke() from the SDK to call an LLM for text generation, dialogue, and similar tasks. For the LLM to drive tool calls, it must output structured arguments that match each tool’s interface—input the tool can accept, derived from the user’s instructions. The method takes the following parameters:
  • model
  • prompt_messages
  • tools
  • stop
  • stream
Method signature:
For the full implementation, see the Invoke Model tab in the sample code below. With this in place, the plugin calls the LLM whenever a user enters a command, builds tool-invocation parameters from the model’s output, and lets the model dispatch the configured tools to complete complex tasks.
Request Parameters for Generating Tools

4. Invoke Tools

Once the model has produced tool parameters, the plugin must actually call the tools. Use session.tool.invoke() to make those requests. The method takes the following parameters:
  • provider
  • tool_name
  • parameters
Method signature:
To let the LLM generate the tool-call parameters itself, feed the model’s extracted tool calls into your invocation code:
Your plugin can now perform Function Calling automatically—for instance, retrieving the current time.
Tool Invocation

5. Create Logs

Complex tasks usually take multiple steps, and you need to track each step’s result to analyze decisions and refine your strategy. The SDK’s create_log_message and finish_log_message let you record state before and after each call, which speeds up problem diagnosis. For example:
  • Log a “starting model call” message before calling the model to show execution progress.
  • Log a “call succeeded” message once the model responds, so its output can be traced end to end.
Once set up, the workflow log shows the execution results:
Agent Output Execution Results
When a task spans multiple rounds, set the parent parameter in your log calls to nest the logs hierarchically and keep them easy to follow:

Sample Code

The following code gives the Agent strategy plugin the ability to invoke the model:

6. Debug the Plugin

With the declaration file and implementation code complete, verify that the plugin runs correctly. Dify supports remote debugging: go to Plugin Management to obtain your debug key and remote server address.
Debug Key and Remote Server Address in Plugin Management
In your plugin project, copy .env.example to .env and fill in the remote server address and debug key.
Then run:
The plugin appears in your workspace, where team members can also access it.
Browser Plugins

Package the Plugin (Optional)

Once everything works, package your plugin by running:
A file named basic_agent.difypkg (matching your plugin name) appears in your current folder. This is your final plugin package. Congratulations! You’ve developed, tested, and packaged your Agent Strategy Plugin.

Publish the Plugin (Optional)

You can now upload the package to the Dify Plugins repository. Before doing so, ensure it meets the Plugin Publishing Guidelines. Once approved, your code merges into the main branch, and the plugin automatically goes live on the Dify Marketplace.

Further Exploration

Complex tasks often need multiple rounds of thinking and tool calls, repeating the model invoke → tool use cycle until the task ends or the iteration limit is reached. Managing prompts well is crucial in this process. See the complete Function Calling implementation for a standardized approach to letting models call external tools and handle their outputs.
Last modified on June 24, 2026