# Teams

> Teams nest to describe your organization; levels are your own labels; a file loads the whole tree.

Teams sit between the organization and the person. Use them to see who spends what, at every level of your
organization.

## Teams nest

A team can have a **parent** team, so you can describe your whole structure: for example a department, the teams in
it, and the squads in those. Up to 10 levels deep, and never a cycle.

```sh
nexus team create engineering --level department
nexus team create platform --parent engineering --level team --name "Platform"
nexus team move platform --parent engineering      # or --root for the top level
nexus team tree --depth 2                          # levels, leads and headcount
```

A team has a slug (unique in your organization, up to 127 characters), a name and an optional description.
Moving a team moves everything under it, and its spend stays attributed to it. Deleting a team that has child teams
needs `--cascade` (API: `?cascade=1`), which deletes the whole subtree; spend always keeps the team it was attributed
to.

## Levels are yours

A team's **level** is a free-text label your organization chooses, such as `department`, `team` or `squad` (1 to 32
lower-case letters, digits or dashes). Nexus gives levels no meaning; they help you read the tree.

## Leads, members and home teams

Owners and admins add people to teams as `member` or `lead`. A person can be in several teams and has at most one
**default team** per organization, their **home team**: the first team they join becomes it, and they can choose
another among their teams.

```sh
nexus team add platform ada@example.com --lead
nexus team use platform                            # your own default team
```

**Pending members.** Adding an email that isn't in the organization yet stores a pending membership (`202`,
`pending: true`), optionally with `--home`. When that person joins, the pending entry becomes a membership. Nothing
is emailed: invite them with `nexus members invite --pending` ([Organizations and roles](/docs/concepts/organizations-and-roles#inviting-people)).

## Import your structure from a file

Owners and admins load teams, leads and members from a YAML or JSON file, in one transaction.

```yaml
units:
  - slug: engineering
    name: Engineering
    level: department
    leads:
      - email: lead@example.com
  - slug: platform
    level: team
    parent: engineering           # absent or null: a top-level team
    members:
      - email: dev@example.com
        home: true                # their default team
      - email: new-hire@example.com   # not in the organization yet: pending
```

Teams are matched by slug: missing ones are created, existing ones moved, renamed and relabelled to match. The same
file twice changes nothing the second time.

**Review first, then apply exactly what you reviewed.** A dry run changes nothing and returns the diff it would apply
and a **plan**, a hash of that diff. Applying with that plan succeeds only if the result is still exactly the plan;
if anything changed in between, Gateway answers `409` `plan_changed` and applies nothing.

```sh
nexus team import teams.yaml --dry-run             # the diff, and a plan
nexus team import teams.yaml --plan 3f9c0a1b2c3d4e5f
```

`--prune` (API: `prune: true`) also removes the memberships, pending entries and teams the file doesn't list (never
spend); applying it needs `--plan` or `--yes`. `--invite` invites the file's people who haven't joined, after the
import. The API is [`POST /v1/teams/import`](/docs/api/teams#import-teams).

## Who can do what

| Who | Can |
| --- | --- |
| Owners and admins | Create, rename, move and delete teams; add and remove members; import; set anyone's default team; set team caps |
| Team leads | In the teams they lead and those under them: narrow the model policy, pause or limit model traffic, read the audit events |
| Everyone | See the organization's teams and members; set their own default team among their teams |

## Model policies and budgets

Each team can have its own [model policy](/docs/concepts/models-and-policies): it narrows what the organization and
the teams above it allow, and a deny anywhere on the path wins.

Each team can also have a spend cap per day, week or month. A cap counts the team and every team under it. A call
must fit in every cap on the path from the top team down to its own team, else it is refused with `402`. A moved team
keeps its past spend under its old parent.

```sh
nexus budget set team/platform 500/month
nexus budget show team/platform
```

A member of the team sees only what is left for them, never the cap of a team above theirs. Each person can also have
a monthly [spend limit](/docs/concepts/spend-limits).
