Clean architecture llm

From VilnaCRM-Org/user-service · View interactive version

Clean architecture llm is an AI agent skill from the GitHub repository VilnaCRM-Org/user-service. Design LLM-powered modules with Clean Architecture boundaries, SOLID, DRY, KISS, and provider-agnostic ports/adapters. Use when adding prompt workflows, model/provider clients, tool orchestration, AI review automation, agent skills, or other LLM-backed capabilities.

How to use this skill

Skill file

Clean Architecture for LLM Modules

Use this skill before implementing or reviewing any LLM-backed behavior. It

keeps provider SDKs, prompt construction, orchestration, and business logic in

the right places so LLM integrations remain testable and replaceable.

Context

Use this skill for:

Pair this skill with:

Task

Design LLM modules so:

model-specific configuration.

hidden inside handlers.

instead of live model calls.

Layer Rules

| Layer | Owns | Must Not Own |

| -------------- | ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------ |

| Domain | Business rules, value objects, entities, domain events | Provider names, prompts, tokens, HTTP clients, SDK classes, model IDs |

| Application | Use cases, command/query handlers, ports, request/response DTOs, provider-neutral prompt factories | Direct SDK calls, environment reads, logging secrets, live network calls |

| Infrastructure | Provider adapters, SDK/HTTP clients, auth, retries, timeouts, rate-limit handling, telemetry exporters | Business rules, domain decisions, prompt policy decisions |

| Interface/API | Controllers, processors, CLI commands, serializers | Prompt assembly, provider choice, retry policy, domain decisions |

| Tooling/Docs | Agent skills, examples, review checklists, BMAD specs | Runtime secrets or production-only behavior |

Execution Steps

1. Classify the Capability

Write down the business use case in provider-neutral language:

verification."

If the behavior is not business logic and only supports developer workflow,

place it under tooling/docs rather than runtime application code.

2. Define the Port First

Create an Application-layer interface that describes the capability, not the

vendor API. The interface should accept typed request objects and return typed

response objects.

In this repository, "port" means the provider-neutral Application-layer

interface role. Put the interface in an existing class-type directory that

matches its responsibility, such as Provider/, Validator/, or Resolver/,

or get explicit approval before introducing a new Port/ directory.

Required:

LlmService.

Forbidden:

3. Keep Prompt Construction Explicit

Prompts are behavior. Treat them as named collaborators.

Use one of these patterns:

response parsing, and policy checks in separate steps when the workflow is

complex.

Rules:

4. Isolate Provider Details in Adapters

Infrastructure adapters implement Application ports and translate between typed

application DTOs and provider SDK/HTTP payloads.

Adapters must own:

Adapters must not own:

5. Apply SOLID, DRY, and KISS

SOLID:

parsing, and persistence.

every caller.

contract and failure semantics.

depends on Application contracts.

DRY:

parsers through named collaborators.

KISS:

repeated cross-cutting behavior, or measurable complexity.

6. Make Failures Deterministic

LLM calls are probabilistic, but application behavior must not be.

Required:

7. Test Without Live Models

Unit and integration tests must not call live providers.

Use:

Live smoke tests, if needed, must be opt-in, isolated, and excluded from default

CI unless explicitly approved.

8. Protect Privacy and Observability

Required:

request correlation ID.

exists.

not hardcoded in handlers.

9. Verify Before PR

Before opening a PR, confirm:

Design Patterns

| Pattern | Use When | Avoid When |

| --------- | --------------------------------------------------------------- | ------------------------------------------------------ |

| Adapter | Wrapping provider SDK/HTTP APIs behind an Application port | Business rules are being hidden in Infrastructure |

| Strategy | Selecting model, provider, prompt, or safety policy by use case | There is only one fixed path |

| Factory | Building prompts, provider requests, or typed DTOs from inputs | It only forwards parameters without construction logic |

| Decorator | Adding retries, metrics, caching, or redaction around a port | It changes business behavior unexpectedly |

| Pipeline | A workflow has several ordered, independently testable steps | A simple handler would stay clearer |

Anti-Patterns

command handlers.

Supporting Files

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.