# Multi-Function App

> Apps can define multiple entry points to support different modes of operation within the same container context. This reduces cold starts and resource duplication.

**URL:** https://inference.sh/docs/examples/multi-function-app
**Last updated:** 2026-09-24

---

Apps can define multiple entry points to support different modes of operation within the same container context. This reduces cold starts and resource duplication.

> **Full Guide:** See [Multi-Function Apps Developer Guide](/docs/extend/multi-function-apps) for complete details on function discovery rules, schema generation, and debugging.

## Use Cases

*   **LLMs**: Separate `chat` (history-aware) and `completion` (raw text) functions.
*   **Image Models**: `generate`, `upscale`, and `inpaint` functions using the same loaded model.
*   **Data Pipelines**: `process`, `validate`, and `export` steps exposed individually.

## Defining Functions

Define functions as async methods on your `App` class. Each function (except `setup`) is an exposable entry point.

`inference.py`:

```python
from inferencesh import BaseApp, BaseAppInput, BaseAppOutput
from pydantic import Field

# 1. Define Schemas
class GenerateInput(BaseAppInput):
    prompt: str

class UpscaleInput(BaseAppInput):
    image_id: str
    scale: float = 2.0

class ImageOutput(BaseAppOutput):
    uri: str

# 2. Define App
class App(BaseApp):
    async def setup(self, config):
        self.model = load_diffusion_pipeline()

    # Default function (optional, conventionally named 'run')
    async def run(self, input: GenerateInput) -> ImageOutput:
        return await self.generate(input)

    # Named function: generate
    async def generate(self, input: GenerateInput) -> ImageOutput:
        image = self.model(input.prompt)
        return ImageOutput(uri=save(image))

    # Named function: upscale
    async def upscale(self, input: UpscaleInput) -> ImageOutput:
        image = load(input.image_id)
        result = self.model.upscale(image, input.scale)
        return ImageOutput(uri=save(result))
```

## Running Specific Functions

Specify the `function` parameter when calling the app.

### CLI

```bash
# Run 'upscale' function
belt app run user/app-name --input input.json --function upscale
```

### Python SDK

```python
client.run({
    "app": "my-app",
    "function": "upscale",
    "input": {
        "image_id": "img_123",
        "scale": 4.0
    }
})
```

### API

```json
POST /apps/run
{
  "app": "my-app",
  "function": "upscale",
  "input": { ... }
}
```
