Why Java for AI? Where Java fits next to Python

Python trains the models. But most AI work today is building the app around a model, and that is the kind of work Java has done well for decades. Here is an honest map of who does what.

Two sides of an AI product. Left, a sheet titled Making the model, mostly Python: collect and clean data, train for weeks on GPUs, fine-tune and evaluate, with PyTorch and notebooks, done by a few big labs. An arrow leads to a black circle labelled LLM, weights file, reached over HTTP. A two-way arrow connects it to a yellow sheet titled Building the app around it, where Java fits: your Java service, with prompts, tools and rules, connected to users, databases and tools or APIs. Ticks list security, logins and audit; thousands of users at once; the systems you already run. A note says most AI work today is here.
Fig. 1 — Making a model and using a model are two different jobs. Java is built for the second one.

If you are a Java developer, you have probably felt it. Every AI tutorial starts with pip install. Every demo is a Python notebook. It is easy to think that AI is a Python thing and Java is being left behind.

But build one real AI feature and you notice something. Almost none of the work is the model itself. It is everything around it: calling it, giving it the right data, checking what it said, keeping it safe, and plugging it into systems that already existed. That is ordinary backend work. And backend work is what Java does best.

This article is the first part of Java for AI Engineers, a series about the Java skills you need to work with LLMs and build AI agents. We start with the big question: why Java, and where does it really fit?

Two different jobs: making a model and using one

There are two very different kinds of AI work, and most of the “Python vs Java” confusion comes from mixing them up.

Job 1: making the model. Someone collects a huge amount of text, trains a neural network on thousands of chips for weeks, and then fine-tunes it to be a helpful assistant. The result is a file of numbers called weights. This is research and maths-heavy work, and it happens almost entirely in Python, with libraries like PyTorch. Only a small number of companies train the big models you hear about.

Job 2: using the model. Everyone else takes a finished model and builds something with it: a support bot, a tool that reads invoices, a search box that understands questions, an agent that books a meeting. The model sits behind an HTTP endpoint, either on a provider’s servers or on your own machine. Your code sends it text and gets text back.

If you have watched episode 1 of the series, “What is AI, really?”, you saw that an LLM is a model trained to predict what comes next. Job 1 is how that model gets made. Job 2 is what you do with it. For most developers, and most companies, the work is Job 2.

“Most AI work today is not training models. It is calling one, and building a good app around it.”

What “building around the model” really means

Here is what an AI feature needs once the model exists. Look at the list and notice how much of it is normal software engineering:

  • Call the model over HTTP, handle timeouts, rate limits and errors.
  • Build the prompt from your data: the user’s question, their account, the right documents.
  • Turn the answer into data your code can use, like a Java object instead of a paragraph.
  • Give the model tools: let it look up an order, check a calendar or call an API, safely.
  • Remember the conversation, because the model itself remembers nothing between calls.
  • Search your documents so the model answers from your facts, not its guesses (this is called RAG).
  • Run it for many users at once, log it, monitor it, test it, secure it and control the cost.

Only the first line talks to the model directly. The rest is integration, data handling, concurrency, security and operations. Those are exactly the problems that big Java systems have been solving for decades.

Agents are even more Java-shaped

An AI agent is an LLM that can take actions. Episode 1 put it in one line: LLM + tools + a loop. The model decides the next step, your code runs that step (search, look up, book), the result goes back to the model, and the loop continues until the job is done.

Look at who does the work in that loop. The model only decides. Your code does everything else: it defines the tools, checks the inputs, calls the real systems, handles failures, asks a human for approval when needed, and keeps a record of what happened. In a company, those real systems are very often Java services already. Building the agent in the same language, close to those systems, is a natural fit.

Why Java is a good fit

Your systems are already in Java. Banks, insurers, retailers, telecoms and governments run huge amounts of Java. Adding AI to a Spring Boot service you already own is much easier than adding a new Python service, a new deployment pipeline and a new team skill just to make an HTTP call.

Types catch mistakes early. LLM output is messy text. Java lets you say exactly what you expect back, for example record Invoice(String number, double total), and fail loudly when the answer doesn’t fit. When an agent is about to issue a refund, “it compiled and the types match” is a comfort.

Waiting is cheap now. An LLM call can take several seconds. A busy app may have thousands of these calls waiting at the same time. Since Java 21, virtual threads let you write simple blocking code and still wait on thousands of calls without running out of threads.

It is built for production. Logging, metrics, tracing, security, connection pools, dependency injection, long-term support releases: the boring things that keep a service running at 3 a.m. are mature in Java.

The AI libraries have arrived. A few years ago this argument was weaker, because the Java AI tools were thin. That has changed. More on this below.

Where Python is still the better choice

Being honest matters here. Python is the better tool when you are:

  • Training or fine-tuning models. The research world, the GPU libraries and the training tools are all in Python.
  • Exploring data in notebooks. For quick experiments and charts, Jupyter is hard to beat.
  • Using the very newest research. New techniques and libraries usually appear in Python first, and Java gets them later.

So it is not “Java instead of Python”. A common and healthy setup is: Python for model and data work, Java for the product that real users touch. And if you only ever call a model over HTTP, the model doesn’t care what language the request came from.

The Java AI toolbox today

Here is the landscape as of November 2026. You don’t need all of it, and this series introduces each piece when we need it.

Tool What it is for
java.net.http.HttpClient + Jackson Talking to any LLM API with plain Java. No framework. Part 3 of this series.
Official SDKs OpenAI, Anthropic (Claude) and Google publish their own Java libraries for their APIs.
Spring AI The Spring team’s AI project. Chat, prompts, structured output, tools, memory, RAG, with Spring Boot auto-configuration. Version 2.0 shipped in 2026.
LangChain4j A popular independent Java AI library with “AI Services”, memory, tools, RAG and agents. Works with or without Spring.
MCP Java SDK The official Java SDK for the Model Context Protocol, for building tool servers and clients that any AI app can use. Built together with the Spring AI team.
Agent frameworks Google’s Agent Development Kit (ADK) for Java reached 1.0 in March 2026. Embabel, from Spring’s creator Rod Johnson, reached 1.0 in July 2026.
Local models Ollama runs models on your laptop and Java talks to it over HTTP. There are also pure-Java and ONNX-based options for running models inside the JVM.

Version numbers move fast in this space, so check each project’s site before you start. The ideas in this series stay the same.

What you need to learn (and what you don’t)

You don’t need to learn linear algebra, train a neural network, or switch to Python. You do need a few things, and they are the plan for this series:

  1. How LLMs behave, from a developer’s point of view. Tokens, context windows, temperature, and why the model forgets everything between calls. (Part 2.)
  2. How to call an LLM with plain Java. Before any framework, see the actual HTTP request and JSON. (Part 3.)
  3. The modern Java features that make AI code clean. Records, sealed types, pattern matching, text blocks and virtual threads. (Part 4.)
  4. Later in the series: Spring AI and LangChain4j, structured output, tool calling, MCP, RAG with vector databases, and finally agents, plus the production skills (cost, testing, security, observability).

If you know Java and REST APIs, you already have more than half of what an AI engineer needs. The rest is learnable in weeks, not years.

Key takeaways

  • There are two AI jobs: making a model (mostly Python) and using one (any language that can make an HTTP call).
  • Most AI work today is the second job: prompts, data, tools, memory, safety and scale around a model.
  • That work is integration and backend engineering, which is where Java is strongest.
  • Agents are LLM + tools + a loop. The model decides; your code does the work, often against Java systems.
  • Python stays the right tool for training, fine-tuning and notebooks. Many teams use both.
  • The Java AI ecosystem is now mature: official SDKs, Spring AI, LangChain4j, the MCP Java SDK and agent frameworks.

Frequently asked questions

Is Java good for AI?

Java is very good for building AI applications and agents: the services that call an LLM, connect it to data and tools, and run it for many users. It is not the usual choice for training models from scratch, which is mostly done in Python.

Should I learn Python or Java for AI?

If you want to train or research models, learn Python. If you want to build AI features into real products, and you already know Java, stay with Java and learn how LLMs work. Many AI engineers end up reading some Python anyway, because examples are often written in it.

Can Java call ChatGPT, Claude or Gemini?

Yes. They are all reached over HTTP APIs, and OpenAI, Anthropic and Google each publish a Java SDK. You can also call them with Java’s built-in HttpClient, which we do in part 3 of this series.

What is the best Java library for LLMs?

The two main choices are Spring AI and LangChain4j. Spring AI fits naturally if your app is already Spring Boot. LangChain4j works in any Java app and has many integrations. Both are good; this series covers both later.

Can I run an LLM inside Java without the internet?

Yes. The easiest way is to run the model with Ollama on your own machine and call it from Java on localhost. That keeps your data on your computer and costs nothing per request.

Next in the series

Next, we look inside the model from a Java developer’s point of view: How LLMs work: tokens, context windows and temperature. You will see why the model forgets everything, why long chats get expensive, and which settings to use for which job.

See it in action

Comments

LEAVE A COMMENT
No sign-up. Comments appear after a quick review.