A function runs code you write in an [environment](/docs/environments), with no agent and no model. Each start is a run. A run calls your handler with an input and saves what it returns.

## Define a function

```yaml title=".ellipsis/functions/stale-pull-requests.yaml"
ellipsis:
  kind: function
  name: stale-pull-requests

trigger:
  type: cron
  schedule: '0 9 * * 1'
  input: {days: 14}

input:
  json_schema:
    type: object
    properties:
      days: {type: integer, minimum: 1}
    required: [days]

function:
  handler: api-repo/functions/stale_pull_requests.py:handler
  environment:
    repositories:
      - name: api-repo
  permissions:
    github:
      - read_only
  timeout: 2m
```

```python title="api-repo/functions/stale_pull_requests.py"
import json
import subprocess
from datetime import datetime, timedelta, timezone

def handler(context, input):
    cutoff = datetime.now(timezone.utc) - timedelta(days=input["days"])
    listed = subprocess.run(["gh", "pr", "list", "--json", "number,title,updatedAt"], capture_output=True, text=True, check=True)
    stale = [pr for pr in json.loads(listed.stdout) if datetime.fromisoformat(pr["updatedAt"]) < cutoff]
    return {"stale": stale}
```

Every Monday at 09:00 UTC this function lists the open pull requests in `api-repo` that haven't changed in 14 days, and the run saves them as its return value.

## Deploy from git

Commit a `.yaml` or `.yml` file with `ellipsis.kind: function` anywhere under `.ellipsis/` on the default branch. It appears on the dashboard's **Functions** page. Your organization can also keep these files in a repository named `.ellipsis`.

If an update is invalid, the last valid version keeps running and the function's page shows the error. Deleting the file disables the function.

`POST /v1/functions` creates a function without a repository file. Send the same fields as JSON under `function`. Each function is edited in one place, its git file or the API. Use `link` to move an API-created function into git, or `unlink` to edit a git-managed one through the API.

## Write the handler

`function.handler` is a file and a function name joined by `:`. The path starts at `/sandbox`, so it begins with the repository name. The handler runs with that repository as its working directory.

A `.py` file runs on Python 3.11. A `.js` or `.ts` file runs on Node 22, which strips TypeScript types with no build step. The JavaScript handler is a named export. To run on another program, such as `bun` or `api-repo/.venv/bin/python`, set `function.interpreter`.

The handler is called as `handler(context, input)` and may be async.

- `input` is the run's input, checked against `input.json_schema`.
- `context.function` has the function's `id`, `name`, and `sha`.
- `context.run` has the run's `id`, `source`, `created_at`, `scheduled_at`, and `timeout_at`.
- `context.event` is the webhook that started the run, as the integration sent it. It's null for other starts.
- `context.pull_request` is `{"number": 42}` for a run a `pull_request` trigger started, and null otherwise.

The handler returns a JSON object or nothing, at most 256 KiB. Set `function.output.json_schema` to check it. A return value that fails the schema fails the run with reason `invalid_output`.

For a short handler, write it in the config with `function.code`. `function.handler` then names only the file it's saved as.

```yaml title=".ellipsis/functions/add.yaml"
ellipsis:
  kind: function
  name: add

function:
  handler: add.py:handler
  code: |
    def handler(context, input):
        return {'sum': input['a'] + input['b']}
```

Inline code can't import a repository's files. A handler that needs them is a file in the repository.

## Start a run

Choose **Run** on the function's dashboard page, or start it through the API:

```python
import os
from ellipsis import Ellipsis

client = Ellipsis(api_key=os.environ["ELLIPSIS_API_TOKEN"])
response = client.functions.runs.create("stale-pull-requests", input={"days": 7}, wait=True)
print(response.run.status, response.run.output)
```

```typescript
import { Ellipsis } from '@ellipsis-dev/sdk';

const client = new Ellipsis({ apiKey: process.env.ELLIPSIS_API_TOKEN! });
const { run } = await client.functions.runs.create('stale-pull-requests', { input: { days: 7 }, wait: true });
console.log(run.status, run.output);
```

```bash
curl https://api.ellipsis.dev/v1/functions/stale-pull-requests/runs -H "Authorization: Bearer $ELLIPSIS_API_TOKEN" -H "Content-Type: application/json" -d '{"input": {"days": 7}, "wait": true}'
```

A run starts in `queued`. With `wait`, the request holds for up to 45 seconds and returns the run as it is then. Without it, read the run with `GET /v1/functions/{id}/runs/{run_id}` until its `status` is final. `GET /v1/functions/{id}/runs/{run_id}/logs` returns what the handler wrote to standard output and standard error, the last 256 KiB.

## Triggers

A function has at most one trigger. Without one, it runs only when you start it. A `cron` trigger starts a run on its schedule. A `react` trigger starts one run for each GitHub, Linear, Sentry, or Slack event it matches. Both take the same fields as an agent's [triggers](/docs/agents/triggers), plus `input`, the input each run starts with.

A run a `pull_request` event starts runs the default branch's code. It doesn't check out the pull request.

## Statuses

| Status | Meaning |
| --- | --- |
| `queued` | Waiting to start. |
| `preparing` | Its environment is starting. |
| `running` | The handler is running. |
| `completed` | The handler returned. `output.result` has the return value. |
| `failed` | The run ended with an error. `error.reason` says why. |
| `cancelled` | The run never ran, for example because the queue was full. |

A failed run's `error.reason` is one of these, among others.

| Reason | Cause |
| --- | --- |
| `handler_failed` | The handler raised an error, or its file or function wasn't found. |
| `timeout` | The handler ran past `function.timeout`. |
| `invalid_output` | The return value isn't a JSON object, is over 256 KiB, or fails `function.output.json_schema`. |
| `lifecycle_hook_failed` | An environment hook failed. |
| `queue_full` | 100 runs were already waiting. |

## Limits

- `function.timeout` is 1 second to 15 minutes, default 1 minute. It covers loading the file and the call.
- An account runs up to 10 runs at once. More wait in the queue, up to 100.
- An account starts up to 60 runs a minute. A start through the API past either limit fails with HTTP 429.
- A run is charged for CPU and memory from when its environment starts. See [Pricing](/docs/pricing).
- An environment's `mcp_servers`, `compute.timeout`, and `compute.idle` don't apply to a function.
- A GitHub permission rule in a function can't set `limit` or use `{session_id}`.
