← BACK TO BLOG

Hexagonal architecture and language models: flexibility and robustness in AI

Hexagonal architecture and language models: flexibility and robustness in AI

When we talk about systems with language models (LLMs), we almost always focus the conversation on the model: how powerful it is, how many parameters it has, how it’s trained, or how much it costs to use. But there’s something equally important that we often overlook: the architecture that surrounds it. Because a good model with bad architecture can end up being a maintenance nightmare. And that’s where hexagonal architecture, also known as Ports & Adapters, comes in.

What is Hexagonal Architecture?

Hexagonal architecture was proposed by Alistair Cockburn with a simple yet powerful idea: an application should not depend directly on its environment (databases, APIs, interfaces, etc.), but rather communicate with it through well-defined ports and interfaces. The goal is to isolate the system’s core logic, the part that truly provides value, from any technical or infrastructure details surrounding it. In other words: your application should remain alive even if you change everything around it.

Why a Hexagon?

The hexagon is merely a visual metaphor. Cockburn chose it because it allows multiple entry and exit points to the core of the application. Each side of the hexagon represents a port, and each port connects to adapters that translate between the external and internal worlds.

This way, the core remains clean, stable, and protected from technological changes.

Main Components

To understand how it works, let’s look at its three basic elements:

1. Core (Domain)

It’s the heart of the system. This is where the business logic lives or, in the case of AI, the logic of the agent or cognitive flow. The core knows nothing about APIs, databases, or models. It only defines what needs to be done and how it’s organized internally.

2. Ports

These are abstract interfaces that define how the core communicates with the outside world. They can be:

3. Adapters

These are the concrete implementations that act as “translators” between the system’s domain and the external technology, connecting those ports to the real world. For example:

General Advantages of Hexagonal Architecture

Advantages of Hexagonal Architecture in LLM-Based Systems

When we apply this pattern to the world of generative artificial intelligence, its advantages become even more apparent.

Conclusion

Hexagonal architecture isn’t just a nice way to draw diagrams; it’s a design philosophy that protects the essence of your system from external chaos. In a world where language models evolve daily, this pattern allows you to build solutions that are more flexible, scalable, and durable.

Applying it to AI projects isn’t a trend, it’s an investment in clarity, stability, and the future.