---
sidebar_position: 1
question: How do I create a module, run it and deploy it?
kind: guide
---

# Creating a Module

A module is the unit you build on ABI. It runs in its own process and reaches the engine only through NATS, with `naas-abi-sdk`. It can hold agents, models, workflows, pipelines, integrations, ontologies, apps and jobs, and it deploys, restarts and scales on its own. How it works underneath is in [Remote modules](/architecture/remote-modules).

This page builds one in a project made by `abi new project`, runs it with `abi dev up`, and deploys it with Docker Compose.

## Before you start

- A project on NATS: `abi new project <name>` creates one (see the [Quickstart](/capabilities/get-started/quickstart)). For a 2.x project, follow [Migrate from 2.x to 3.0](/capabilities/upgrade/migrate-to-3) to phase 2 first.
- `nats-server` 2.14 or later on `PATH`, for `abi dev up`.

## Create the module

From the project's root:

```bash
abi new module research
```

```text
Remote module 'research' created at:
  .../src/research

It runs in its own process and registers itself in discovery (project 'my_project'), so it is not added to `modules:`.
- `abi dev up` runs it: added to dev.modules in config.yaml.
- Its module key is in .env (ABI_MODULE_KEY_RESEARCH): it signs its tokens as my_project.research only.
- `abi start` runs it: added the 'research' service to docker-compose.override.yml.
```

What it wrote:

| Path | What |
|---|---|
| `src/research/__init__.py` | `ABIModule`: its id, configuration, dependencies, agents, jobs, a sample workflow and the lifecycle |
| `src/research/__main__.py` | `python -m research`: `run_module` with discovery and signal handling |
| `src/research/agents/ResearchAgent.py` | An SDK `IntentAgent` on the engine's models, with its memory in the Document Service |
| `src/research/jobs.py` | Job and workflow bodies, testable without a broker |
| `src/research/workflows/`, `pipelines/`, `integrations/` | Empty packages, each with an `AGENTS.md`: `abi new workflow`, `abi new pipeline` and `abi new integration` write into them |
| `src/research/ontologies/modules/ResearchOntology.ttl` | A starter ontology, sent to the engine's schema graph |
| `src/research/apps/research/` | A web app (`manifest.json`, `index.html`) |
| `src/research/tests/` | Offline tests |
| `src/research/Dockerfile` | An image with the SDK only: the build fails if `naas_abi_core` is installed |
| `config.yaml` | A `dev.modules` entry, for `abi dev up` |
| `docker-compose.override.yml` | A `research` service, for Docker Compose |
| `.env` | `ABI_MODULE_KEY_RESEARCH`, the module's key |

Module names must match `^[a-z][a-z0-9-]{0,62}$` once kebab-cased. Outside a project, or with `--standalone`, the command creates a repository of its own (see [A module in its own repository](#a-module-in-its-own-repository)).

:::note Project without a `nats:` block
`abi new module` makes this kind of module in a project whose config has a `nats:` block, which `abi new project` writes. In an older project without one, add `--remote`; the module runs once the project is on NATS ([Migrate from 2.x to 3.0](/capabilities/upgrade/migrate-to-3)).
:::

The module is never listed under `modules:` in `config.yaml`: it registers itself in discovery when its process starts. `modules:` is where you enable [marketplace modules](/capabilities/build/modules/marketplace/marketplace-install).

## The module class

```python title="src/research/__init__.py"
from dataclasses import dataclass
from datetime import timedelta
from typing import Any

from naas_abi_sdk import AgentDescriptor, BaseModule, ModuleConfiguration, ModuleDependencies
from naas_abi_sdk.jobs import Every, JobContext, job

from research import jobs
from research.agents.ResearchAgent import DESCRIPTION, NAME, create_agent

MODULE_ID = "my_project.research"


@dataclass
class ResearchConfiguration(ModuleConfiguration):
    chat_model: str = ""  # a canonical model id the engine registers; empty: its default


class ABIModule(BaseModule[ResearchConfiguration]):
    Configuration = ResearchConfiguration
    module_id = MODULE_ID
    package_version = "0.1.0"
    # Services this module calls on the engine. Undeclared ones are refused.
    dependencies = ModuleDependencies(services=("model_registry", "document", "triple_store"))
    # Published in discovery; Nexus lists them with the engine's agents.
    agents = (AgentDescriptor(NAME, DESCRIPTION, capabilities=("agent.invoke.v1",)),)

    async def on_initialized(self) -> None:
        # The engine's services are reachable; bind what the module declared.
        agent = await create_agent(self.engine, MODULE_ID, chat_model=self.configuration.chat_model)
        self.expose_agent(NAME, agent.as_handler())

    @job(triggers=(Every("1h"),), timeout=timedelta(minutes=5))
    async def heartbeat(self, ctx: JobContext) -> dict[str, Any]:
        return await jobs.heartbeat(self, ctx)

    async def run(self) -> None:
        ...  # waits until READY, then stays up until stopped
```

This is the scaffold, shortened: the generated file also has a daily `@workflow` example and the body of `run()`.

The rules:

- **Never import `naas_abi_core`** (nor `naas_abi`, `naas_abi_marketplace`). The module's tests and its Docker build check it.
- **Keep `module_id` stable.** Discovery, job subjects and the agent's memory are keyed by it, and the module's key signs for it only.
- **Declare every service** you call in `ModuleDependencies(services=...)`: `activity_log`, `bus`, `cache`, `coding_environment`, `dataset`, `document`, `email`, `event`, `keyvalue` (or `kv`), `model_registry`, `object_storage`, `secret`, `source_control`, `triple_store`, `vector_store`. Calls are async: `await self.engine.services.object_storage.put_object(prefix, key, data)`. Keep `triple_store` while the module has files under `ontologies/`: without it, the module stops at start. To call another module, name it in `modules=(...)`. See [Add dependencies](/capabilities/build/modules/add-dependencies).
- **Bind in `on_initialized`** every agent, app and job handler the class declares: the module becomes `READY` right after.
- Hooks run in this order: `on_load`, `on_initialized`, `run()`, then `on_unloaded` at the end. Stop your own background tasks in `on_unloaded`.
- Outside the class, `current_module()` (from `naas_abi_sdk`) returns the running module for the current task.

### What the module finds by itself

- **Workflows and pipelines**: every `Workflow` subclass under `workflows/` and every `Pipeline` subclass under `pipelines/` (recursively; `*_test.py` and `test_*.py` are left out). Its name is the class name in snake case. The module builds each one after `on_initialized`, with `New(module)` when the class defines it, else with an empty configuration; bind one that needs arguments with `self.expose_workflow(name, instance)` (or `expose_pipeline`). Each one is also a durable workflow (`workflow.<name>`, `pipeline.<name>`) that other modules, Nexus and scripts start by name. See [Create a workflow](/capabilities/build/agents/creating-a-workflow) and [Create a pipeline](/capabilities/build/knowledge-graph/creating-a-pipeline).
- **Ontologies**: every `.ttl` file under `ontologies/` (a path with `sandbox` in it is skipped), sent to the engine's schema graph once the module is `READY`. The module must declare `triple_store`. See [Create an ontology](/capabilities/build/knowledge-graph/creating-an-ontology).
- **Apps**: every `apps/<name>/manifest.json`, listed in Nexus while the module runs.

Agents, models and jobs are declared on the class (`agents`, `models`, `@job`) and bound in `on_initialized`. Integrations under `integrations/` are classes your workflows, pipelines and agents build themselves (see [Create an integration](/capabilities/build/agents/creating-an-integration)).

From the module's folder, `abi new workflow`, `abi new pipeline` and `abi new integration` write `naas_abi_sdk` classes with a test beside each, in the folder the module reads.

## Add an agent

From the module's folder:

```bash
cd src/research
abi new agent Reviewer agents
```

It writes `agents/ReviewerAgent.py`, an SDK agent. Declare it and bind it:

```python
from research.agents import ReviewerAgent

class ABIModule(BaseModule[ResearchConfiguration]):
    agents = (
        AgentDescriptor(NAME, DESCRIPTION, capabilities=("agent.invoke.v1",)),
        AgentDescriptor(ReviewerAgent.NAME, ReviewerAgent.DESCRIPTION, capabilities=("agent.invoke.v1",)),
    )

    async def on_initialized(self) -> None:
        ...
        reviewer = await ReviewerAgent.create_agent(self.engine, MODULE_ID)
        self.expose_agent(ReviewerAgent.NAME, reviewer.as_handler())
```

The agent uses the engine's models over NATS (no provider key in the module) and keeps its conversations in the engine's Document Service, so they survive restarts and are shared by replicas. Any object with an async `invoke(prompt, context)` (and optionally `stream_invoke`) can be exposed as an agent too. More in [Create an agent](/capabilities/build/agents/creating-an-agent).

To call another module's agent, declare the module and ask discovery:

```python
dependencies = ModuleDependencies(
    services=("model_registry", "document", "triple_store"),  # keep the scaffold's services
    modules=("my_project.other",),
)

async def ask(self, question: str) -> str:
    other = self.engine.modules["my_project.other"]
    agent = await other.get_agent("Other")
    return await agent.invoke(question)
```

## Add a job

A job is an async method with `@job`. JetStream schedules it, and one replica of the module runs each trigger:

```python
from datetime import timedelta
from naas_abi_sdk.jobs import Cron, Every, JobContext, job

@job(triggers=(Cron("0 0 6 * * *", time_zone="UTC"),), timeout=timedelta(minutes=30), max_attempts=3)
async def morning_digest(self, ctx: JobContext) -> dict:
    items = await collect(self)
    if not items:
        ctx.skip("nothing new")
        return {}
    ctx.log(f"{len(items)} items")
    return {"items": len(items)}  # stored on the run record
```

- `Cron` takes **six fields, seconds first** (`"0 0 6 * * *"` is 06:00:00). `Every("15m")` and `OnEvent(event_type=..., filter=...)` are the other triggers.
- Delivery is at least once: a job must tolerate running twice.
- Trigger it from the module with `self.trigger_job("morning_digest", {...})` (`await self.atrigger_job(...)` in async code), from the System app's **Jobs** tab (**Run now**), or with `POST /api/admin/system/jobs/my_project.research/morning_digest/trigger` and `{"payload": {...}}` as a super admin.

Triggers, retries, idempotency and retention are in [Module jobs](/capabilities/build/modules/module-jobs) and, in depth, in [Remote modules](/architecture/remote-modules#module-jobs).

## Add an app

```bash
cd src/research
abi new app dashboard apps
```

It writes `apps/dashboard/` (`manifest.json`, `index.html`). Nexus lists the app while the module runs; enable it on a workspace's Apps page to open it. The files of the folder are served as they are. For a backend, bind an ASGI application:

```python
from fastapi import FastAPI, Request

api = FastAPI()


@api.get("/api/hello")
async def hello(request: Request) -> dict:
    abi = request.scope["abi"]
    return {"user": abi["user_id"], "workspace": abi["workspace_id"]}


class ABIModule(BaseModule[ResearchConfiguration]):
    async def on_initialized(self) -> None:
        ...
        self.expose_app("dashboard", api)  # apps/dashboard/manifest.json
```

The page calls it with a relative URL, `fetch("api/hello")`. It runs sandboxed: no `localStorage` or `document.cookie`, and absolute URLs start with the app's `root_path`. Large uploads stream (`request.stream()`), and so do `StreamingResponse`s. Details and limits: [Remote apps](/architecture/remote-apps).

The module's image installs the SDK only. Add what your backend needs (here `fastapi`) to the `pip install` line of `src/research/Dockerfile`. See [Add dependencies](/capabilities/build/modules/add-dependencies).

## Run it with `abi dev up`

```bash
abi dev up
```

It starts `nats-server`, Oxigraph, the API (with discovery), Nexus web, then every `dev.modules` entry, each under a supervisor that restarts it if it exits. The module gets `ABI_NATS_URL` (logged in as the broker user `module`), `ABI_MODULE_KEY`, `ABI_DISCOVERY_PROJECT` and, with `--with-tracing`, `OTEL_EXPORTER_OTLP_ENDPOINT`.

```bash
abi dev status                        # services and dev modules, with their pids
abi dev logs research -f              # the module's log: "... READY: registered in discovery"
abi dev down --service research       # stop the module only
abi dev up -d --service research      # start it again, next to the running stack
```

The `dev.modules` entry `abi new module` added:

```yaml
dev:
  modules:
    - name: research            # process name: logs, --service
      module: research          # run as `python -m research`
      identity: my_project.research   # its module id (default: its ABIModule's)
```

Each entry also takes `args`, `env` (values may name `${ABI_NATS_URL}` and the other variables), `enabled` and `restart` (`on-failure`, `always`, `never`).

Once the module is `READY`, Nexus lists its agent, the System app shows it on the **Modules** tab and its job on the **Jobs** tab, and its app appears on the Apps page.

Test it offline, with no broker:

```bash
uv run --with pytest pytest src/research/tests
```

More in [Test a module](/capabilities/build/modules/test-a-module).

## Deploy it with Docker Compose

`abi new module` added a service to `docker-compose.override.yml`, which Compose merges into the project's `docker-compose.yml`:

```yaml
services:
  research:
    build:
      context: ./src/research
      additional_contexts:
        abi_proto: ./.abi/libs/naas-abi-proto
        abi_sdk: ./.abi/libs/naas-abi-sdk
    restart: unless-stopped
    environment:
    - ABI_NATS_URL=nats://module:${NATS_MODULE_PASSWORD}@nats:4222
    - 'ABI_MODULE_KEY=${ABI_MODULE_KEY_RESEARCH:?...}'
    - ABI_DISCOVERY_PROJECT=my_project
    - LOG_LEVEL=${LOG_LEVEL:-INFO}
    depends_on:
      abi:
        condition: service_healthy
    networks:
    - abi-network
```

Build and start it with the stack:

```bash
docker compose up -d --build
docker compose logs -f research
```

- The image is built from the module's folder alone, with the SDK and proto packages from the `.abi` submodule; the project root is never sent.
- `abi deploy local --regenerate` never touches `docker-compose.override.yml`.
- **Replicas** (`docker compose up -d --scale research=2`) share the module's agent runs and jobs: one replica takes each job trigger.
- **Health probes:** set `ABI_HEALTH_PORT` (for example `8080`) to open `GET /health` (liveness) and `GET /ready` (200 only when discovery reports `READY`).
- **Tracing:** set `OTEL_EXPORTER_OTLP_ENDPOINT` (see [Observability](/developers/architecture/observability)).
- **Rollouts:** set `ABI_ROLLOUT_ID` per deploy so the previous version keeps serving until the new one is ready, then drains.
- `SIGTERM` drains the module: it stops taking new work, finishes what it started, and unregisters.

## Run it by hand

Any supervisor can run the module. It needs the broker and its key:

```bash
ABI_NATS_URL=nats://module:<NATS_MODULE_PASSWORD>@127.0.0.1:4222 \
ABI_MODULE_KEY=$(abi module key my_project.research) \
ABI_DISCOVERY_PROJECT=my_project \
python -m research
```

`abi module key <module_id>` derives the key from the project's `NATS_JWT_SECRET` (`$NATS_JWT_SECRET`, else `./.env`). Never give a module `NATS_JWT_SECRET` itself. A module whose package has no `__main__` runs with the SDK's runner instead: `naas-abi-module <package> --config module.json`, where the JSON file holds the fields of the module's `Configuration`.

## A module in its own repository

```bash
abi new module research --standalone
```

It creates `./research/` with a `pyproject.toml`, a `Dockerfile` and a README, for a team that ships the module separately. Until `naas-abi-sdk` and `naas-abi-proto` are on PyPI, it installs them from the ABI repository, pinned to the CLI's release tag (`--abi-ref` picks another branch, tag or commit). The project that runs the engine gives it its key (`abi module key <module_id>`) and the broker's `module` password.

## Reference

- [`libs/naas-abi-sdk`](https://github.com/jupyter-naas/abi/tree/main/libs/naas-abi-sdk) in the ABI repository: the SDK source. Its README has the design notes (facades, invocation status and replay, transfers, discovery timing).
- [`libs/naas-abi-proto`](https://github.com/jupyter-naas/abi/tree/main/libs/naas-abi-proto): the protobuf contracts and NATS subjects.
- [`abi new module`](/user-guide/reference/cli#abi-new-module) in the CLI reference.
