<a id="cmf-flink-agents"></a>

# Run Apache Flink Agents on Confluent Platform for Apache Flink

Apache Flink® Agents is an open-source library for building AI agent applications that run as Apache Flink jobs.
This library enables you to create agentic workflows, such as ReAct agents and tool-calling pipelines,
which execute as scalable, fault-tolerant Apache Flink streaming applications.

You can run Apache Flink Agents applications on Confluent Platform for Apache Flink by packaging them as custom Docker images
and submitting them through Confluent Manager for Apache Flink (CMF), following the same pattern as any Flink application.

#### IMPORTANT
Apache Flink Agents is an early-stage open-source project provided by the Apache Flink community.
It is not a Confluent product and is not supported by Confluent.
For issues with the Apache Flink Agents library, see the
[Apache Flink Agents documentation](https://nightlies.apache.org/flink/flink-agents-docs-latest/)
and the [flink-agents GitHub repository](https://github.com/apache/flink-agents).

For examples of how to use Apache Flink Agents, see the
[examples directory](https://github.com/apache/flink-agents/tree/main/examples)
in the Apache Flink Agents repository.

## Prerequisites

- Confluent Platform for Apache Flink installed with Confluent Manager for Apache Flink and the Flink Kubernetes Operator.
  For installation instructions, see [Install Confluent Manager for Apache Flink with Helm](../../installation/helm.md#install-cmf-helm).
- A CMF environment configured. For more information, see [Configure Environments in Confluent Manager for Apache Flink](../../configure/environments.md#cmf-environments).
- [The Apache Flink Agents repository](https://github.com/apache/flink-agents) cloned and built.
  See the [Flink Agents getting started guide](https://nightlies.apache.org/flink/flink-agents-docs-latest/docs/get-started/overview/)
  for build instructions.

## Build a custom Docker image

Apache Flink Agents applications require the Apache Flink Agents distribution JAR and your application JAR
to be available on the Flink classpath. Package these into a custom Docker image based on the
Confluent Flink base image.

Apache Flink Agents provides version-specific distribution JARs (for example, `flink-2.1`). Choose the
distribution that matches your Confluent Platform for Apache Flink version.

Example Dockerfile:

```Dockerfile
FROM confluentinc/cp-flink:2.1.1-cp1

# Flink Agents distribution JAR (uber JAR with all agent dependencies)
COPY flink-agents/dist/flink-2.1/target/flink-agents-dist-flink-2.1-0.2-SNAPSHOT.jar /opt/flink/usrlib/

# Your application JAR
COPY target/my-agent-app.jar /opt/flink/usrlib/
```

#### NOTE
Place JARs in `/opt/flink/usrlib/` so they are available to both the JobManager
and TaskManager pods. If your application requires additional connectors or data files,
add them to the image in the same way.

Build the image and make it available to your Kubernetes cluster:

```bash
docker build -t my-flink-agents-app:latest .
```

## Deploy the application with CMF

Create a FlinkApplication specification for your Apache Flink Agents application.

### JSON

```json
{
  "apiVersion": "cmf.confluent.io/v1",
  "kind": "FlinkApplication",
  "metadata": {
    "name": "my-agent-app"
  },
  "spec": {
    "image": "my-flink-agents-app:latest",
    "flinkVersion": "v2_1",
    "flinkConfiguration": {
      "taskmanager.numberOfTaskSlots": "1"
    },
    "serviceAccount": "flink",
    "jobManager": {
      "resource": {
        "memory": "1024m",
        "cpu": 1
      }
    },
    "taskManager": {
      "resource": {
        "memory": "1024m",
        "cpu": 1
      }
    },
    "job": {
      "jarURI": "local:///opt/flink/usrlib/my-agent-app.jar",
      "entryClass": "com.example.MyAgentApp",
      "state": "running",
      "parallelism": 1,
      "upgradeMode": "stateless"
    }
  }
}
```

### YAML

```yaml
apiVersion: cmf.confluent.io/v1
kind: FlinkApplication
metadata:
  name: my-agent-app
spec:
  image: my-flink-agents-app:latest
  flinkVersion: v2_1
  flinkConfiguration:
    taskmanager.numberOfTaskSlots: "1"
  serviceAccount: flink
  jobManager:
    resource:
      memory: 1024m
      cpu: 1
  taskManager:
    resource:
      memory: 1024m
      cpu: 1
  job:
    jarURI: local:///opt/flink/usrlib/my-agent-app.jar
    entryClass: com.example.MyAgentApp
    state: running
    parallelism: 1
    upgradeMode: stateless
```

If your application requires environment variables, such as API keys for Large Language Model (LLM) providers,
pass them through the `podTemplate`:

```json
{
  "spec": {
    "podTemplate": {
      "spec": {
        "containers": [
          {
            "name": "flink-main-container",
            "env": [
              {
                "name": "OPENAI_API_KEY",
                "valueFrom": {
                  "secretKeyRef": {
                    "name": "llm-api-keys",
                    "key": "openai-key"
                  }
                }
              }
            ]
          }
        ]
      }
    }
  }
}
```

Submit the application using the Confluent CLI or REST API:

### Confluent CLI

```bash
confluent flink application create \
  --environment <env-name> my-agent-app.json
```

### REST API

```bash
curl -X POST http://<cmf-host>:8080/cmf/api/v1/environments/<env-name>/applications \
  -H "Content-Type: application/json" \
  -d @my-agent-app.json
```

For more information on creating and managing applications, see [Create Confluent Manager for Apache Flink Applications](create.md#cmf-create-apps) and [Describe, Update and Delete Confluent Manager for Apache Flink Applications](manage.md#cmf-manage-apps).

## Related content

- [Apache Flink Agents documentation](https://nightlies.apache.org/flink/flink-agents-docs-latest/)
- [Apache Flink Agents GitHub repository](https://github.com/apache/flink-agents)
- [How to Package a Flink Job for Confluent Manager for Apache Flink](packaging.md#cmf-package)
- [Create Confluent Manager for Apache Flink Applications](create.md#cmf-create-apps)
- [Describe, Update and Delete Confluent Manager for Apache Flink Applications](manage.md#cmf-manage-apps)
