# API keys

> Get a key, keep it out of your code, check what it can do, and understand the quota.

Every request to the models needs an API key. This page covers where to keep it, how to check it, and how usage is counted.

## Get a key

[Create an account](https://yhatlabs.com/models/signup), make a key on the [API keys page](https://yhatlabs.com/models/account#keys), and request
credits on the Credits page. A key looks like `yh-...`, belongs to a project of your organization, and spends the
organization's credits.

## Organizations, projects and keys

- **Organization:** your company, lab or team. It holds the members, the credits and the projects. You can belong to several.
- **Project:** one use of the models, such as a pipeline, a product or a client. Each project has its own keys and its own
  usage and logs. Every organization starts with a **Default** project, and owners and admins can create more on the
  [Projects page](https://yhatlabs.com/models/account#projects).
- **Key:** belongs to one project. A member's own key is handed to the project if they leave. A **project key** (made by an
  owner or admin) is for production and keeps working when people come and go.

Credits are shared by the whole organization. To stop one project from spending them all, give it a monthly or total
**cap**; each key can also have its own cap. Archiving a project revokes its keys and keeps its usage.

## Keep it out of your code

Set the key as an environment variable. The Python library, the command line and agent skills all read it from there:

```sh
export YHAT_API_KEY="<your-key>"
```

- **Python:** `Client()` reads `YHAT_API_KEY`. You can also pass `Client(api_key="...")`, or keep the key in a `.env` file loaded
  with [python-dotenv](https://pypi.org/project/python-dotenv/).
- **HTTP:** send it as `Authorization: Bearer <key>`.
- Never commit a key, paste it into a shared notebook, or log it. The library never writes it to disk or to its logs.

## Check your key

This confirms the key works and shows which models it can use and how much of its quota is left:

```python
from yhatlabs import Client

yh = Client()  # reads YHAT_API_KEY from the environment
me = yh.whoami()
print(me["models"])
print(me["usage"]["left"])
```

Output:

```
['chakra-ts', 'chakra-tab']
{'monthly': None, 'total': 1000}
```

```bash
yhatlabs whoami
```

Output:

```
{
  "base_url": "https://api.yhatlabs.com",
  "key": "yh-...a1b2",
  "models": [
"chakra-ts",
"chakra-tab"
  ],
  "usage": {
"unit": "series",
"used": {
  "monthly": 1000,
  "total": 1000
},
"quota": {
  "monthly": null,
  "total": 1000
},
"left": {
  "monthly": null,
  "total": 1000
},
"credits": {
  "used": {
    "monthly": 1000,
    "total": 1000
  },
  "quota": {
    "monthly": null,
    "total": null
  },
  "left": {
    "monthly": null,
    "total": null
  }
}
  }
}
```

`models` lists the models the key may call. `usage.left` is what remains of its quota per period: `None` in Python (`null` in
the CLI's JSON) means no cap for that period.

## Tags

Tags add up spend by experiment, pipeline or client. Give a key tags on its edit page (for example `prod` or
`client-acme`); they go on every call it makes. A call can add its own with a header:

```sh
curl https://api.yhatlabs.com/v1/series/forecast \
  -H "Authorization: Bearer $YHAT_API_KEY" \
  -H "X-Yhat-Tags: experiment-12,weekly" \
  -H "Content-Type: application/json" -d @request.json
```

Up to 10 tags per key and 10 per call: lowercase letters, digits and `. _ : -`, at most 40 characters each. Usage shows
spend by tag, and the logs filter by it.

## Credits

Calls spend credits from your organization. Each call is counted in tokens, from the work it causes (a forecast by its history
and horizon, a table call by its rows and columns), and paid in credits at your organization's rate (1 credit per 1M tokens
by default). Amounts are shown with decimals: a small call costs about `0.02` credits, and every reply states its cost in
`usage.credits`. [Limits](https://yhatlabs.com/models/docs/resources/limits#pricing) has the rules, and `POST /v1/estimate` (or
`yh.estimate_table()`) the cost of a call before you make it. Your balance, what refills it and the last 30 days of use are on
the [Credits page](https://yhatlabs.com/models/account#credits).

When the organization has no credits left, calls fail with HTTP 402 and error type `no_credits`; owners request more on the
Credits page. When a key's or a project's cap is reached, calls fail with HTTP 429 and error type `quota_exceeded`. A call
that fails on our side does not count.

Keys we issued directly (for example for a benchmark) carry their own quota instead: forecasts counted in series, tables in
credits, each with an optional monthly and total cap. When one is used up, calls fail with HTTP 429 and error type
`quota_exceeded` (`RateLimitError` in Python), which is not retried.

## Use your own deployment

To send requests to a dedicated or self-hosted deployment instead of the hosted API, set its address:

```sh
export YHAT_BASE_URL="https://your-deployment.example.com"
```

In Python, `Client(base_url=...)` does the same for one client.
