DEV Community

Cover image for Run your first AI agent in Java — for free, with Mistral
sekka
sekka

Posted on • Originally published at datallmhub.github.io

Run your first AI agent in Java — for free, with Mistral

Every "build an AI agent" tutorial I find assumes two things: you write Python, and you have a paid OpenAI key. I write Java and I didn't want to pay just to experiment.

So here's the version nobody writes: a real LLM-backed agent, on the JVM, for free — using Mistral's free tier and AgentFlow4J, an open-source orchestration runtime on top of Spring AI. About ten minutes, start to finish.

Step 1 — Create a Mistral account

Go to console.mistral.ai and sign up (email, Google, or GitHub). You land on La Plateforme, the developer console.

Step 2 — Get a free API key

Open API KeysCreate new key, name it, copy it (you only see it once).

Export it in your shell — never commit it:

export MISTRAL_API_KEY="sk-...your-key..."
Enter fullscreen mode Exit fullscreen mode

mistral-small is covered by the free tier, which is plenty for building and testing.

Step 3 — Add the dependencies

AgentFlow4J ships via JitPack. Add it plus Spring AI's Mistral starter:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.github.datallmhub.agentflow4j</groupId>
    <artifactId>agentflow4j-starter</artifactId>
    <version>v0.7.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-mistral-ai</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Step 4 — Point Spring AI at Mistral

# application.yml
spring:
  ai:
    mistralai:
      api-key: ${MISTRAL_API_KEY}
      chat:
        options:
          model: mistral-small-latest
          temperature: 0.3
Enter fullscreen mode Exit fullscreen mode

Step 5 — Write and run an agent

@Bean
CommandLineRunner demo(ChatClient.Builder chatClientBuilder) {
    Agent assistant = ExecutorAgent.builder()
            .name("assistant")
            .chatClient(chatClientBuilder.build())
            .systemPrompt("You are a concise, helpful assistant. Answer in one sentence.")
            .build();

    return args -> {
        AgentResult result = assistant.execute(
                AgentContext.of("What is backpressure in reactive streams?"));
        System.out.println("\nMistral says: " + result.text() + "\n");
    };
}
Enter fullscreen mode Exit fullscreen mode

Run with MISTRAL_API_KEY set, and you get a real answer from Mistral — your first LLM agent in Java, for free.

Why a runtime, not just a ChatClient?

A single call is easy. Real systems chain agents, pass state, retry, and need guardrails. That's what AgentFlow4J adds on top of Spring AI: graph orchestration, typed state, checkpoint/resume, and governance gates (budget caps, tool/state policies, human approval) — all idiomatic Java, no Python, no sidecar.

The cookbook has five runnable recipes (RAG, ticket triage, web research, Slack bot, batch processing) if you want to go further.


It's open source (Apache 2.0). If you've been wanting to build agents without leaving the JVM, I'd genuinely love your feedback: github.com/datallmhub/agentflow4j

Top comments (0)