> ## Documentation Index
> Fetch the complete documentation index at: https://docs.costgraph.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Query costs with the CostGraph CLI

> Compare cloud spend by provider and service, filter a cost query, and return JSON for scripts

Use the CLI to compare spend across providers and services, narrow a query to one workload, or read costs in a script. Queries use the tenant attached to your credential.

## Requirements

Install the [CostGraph CLI](https://docs.costgraph.ai/costgraph/cli/install.md) and connect a source of cost data to your tenant. A connection with no imported data can return an empty result.

For grouped queries, use a saved browser session or an API key with `focus:read`. Refer to [API keys](https://docs.costgraph.ai/costgraph/cli/api-keys.md) to create a key. The overview and provider-summary commands require a browser session in the current release.

## Group spend by provider

The following command prints month-to-date spend grouped by provider.

```shell theme={null}
costgraph cost get --group-by provider --no-interactive
```

Each row identifies a provider and its current spend, comparison with the preceding period, and share of the result. These are your tenant's costs, not a price estimate for new infrastructure.

### Browse results interactively

Remove `--no-interactive` to open the cost browser in a terminal.

| Key | Action |
| - | - |
| Arrow keys | Select a row. |
| Enter | Open its detail. |
| `/` | Filter the result. |
| `q` | Close the browser and print the table. |

## Compare services within a provider

Put dimensions in the order you want to explore them. The following command groups AWS spend by provider and then service.

```shell theme={null}
costgraph cost get aws --group-by provider,service --no-interactive
```

The `aws` argument selects a value of the first dimension, which is `provider` in this example. It is equivalent to adding `--filter provider:aws`.

You can also use `--provider aws` to filter by provider without changing your grouping.

## Filter a query

Use `--filter` with a dimension and value. The following query groups AWS and Google Cloud spend by service.

```shell theme={null}
costgraph cost get \
  --group-by service \
  --filter provider:aws \
  --filter provider:gcp \
  --no-interactive
```

Repeated values for the same dimension use OR. Filters on different dimensions narrow the query together. Use the provider identifiers present in your data.

Add `--sub-accounts` to break groups down by sub-account. A source must report sub-account attribution before that breakdown contains useful data.

## Choose a period

The default period is `month_to_date`. Use `--period` to select a different window.

| Period | Additional input |
| - | - |
| `month_to_date` | No additional input. |
| `last_7d` | No additional input. |
| `last_30d` | No additional input. |
| `calendar_month` | Set `--month` to a month in `YYYY-MM` format. |
| `custom` | Set `--start` and `--end` to dates or RFC 3339 timestamps. |

### Calendar month

The following command compares providers during October 2026.

```shell theme={null}
costgraph cost get \
  --group-by provider \
  --period calendar_month \
  --month 2026-10 \
  --no-interactive
```

### Custom dates

For a custom window, pass its start and end.

```shell theme={null}
costgraph cost get \
  --group-by provider \
  --period custom \
  --start 2026-10-01T00:00:00Z \
  --end 2026-11-01T00:00:00Z \
  --no-interactive
```

## Choose billed or allocated costs

Grouped queries use the `billed` layer by default. To query the allocation layer, add `--layer allocation`.

```shell theme={null}
costgraph cost get --group-by provider --layer allocation --no-interactive
```

Billed and allocated costs answer different questions. Compare results from the same layer and period. An allocation query needs the relevant allocation data in your tenant.

## Sort and limit results

Sort the top-level rows by current spend and show ten results.

```shell theme={null}
costgraph cost get --group-by service --sort current --limit 10 --no-interactive
```

Use `--sort=-current` for ascending order. Other sort fields include `previous`, `change`, `share`, `usage`, and `name`.

`--limit` limits the rows printed by the CLI. `--group-limit` limits groups at each query level and combines the remaining groups into `other`. Do not treat these options as interchangeable.

## Return JSON for a script

Add `-o json` to skip the browser and return structured data.

```shell theme={null}
costgraph cost get --group-by provider -o json
```

The result includes `totals`, `groups`, and `series`. Each member of `groups` has a `key` and cost measures such as `current` and `previous`.

The following command extracts provider names and current spend. It requires `jq`.

```shell theme={null}
costgraph cost get --group-by provider -o json | jq '.groups[] | {provider: .key, current: .current}'
```

Check the command's exit status before using its output in automation. Refer to [Configuration and output](https://docs.costgraph.ai/costgraph/cli/configuration.md) for credential selection and output settings.

## Read the cost overview

With a saved browser session, the following command prints total spend, comparison with the preceding period, and provider totals.

```shell theme={null}
costgraph cost --no-interactive
```

Running `costgraph cost` without a subcommand uses month-to-date spend. `costgraph cost overview` offers the same overview and can prompt for a period in an interactive terminal.

### Provider totals

Print providers ranked by spend with a saved browser session.

```shell theme={null}
costgraph cost providers --period last_30d --no-interactive
```

These two summary commands do not accept an API key in the current release. For an API-key workflow, use `costgraph cost get --group-by provider`.

## Check an empty result or access error

Confirm the selected tenant, period, filters, and layer. A successful query can return no groups when the source has no data in that window.

If CostGraph rejects the credential, check [Resolve permission errors](https://docs.costgraph.ai/costgraph/cli/api-keys.md#resolve-permission-errors). A saved browser session takes precedence over an API key set in the environment.

## Command reference

Commands are shown without the `costgraph` prefix. Add `--help` for the complete flag list.

### Cost query commands

Run each command with the `costgraph` prefix. `costs` is an alias for `cost`.

**cost get \[name]** `command`

Queries grouped spend. Accepts a session or a key with `focus:read`.

**cost overview** `command`

Shows spend, period comparison, and provider totals. Requires a session. Bare `cost` uses month-to-date without prompting.

**cost providers** `command`

Ranks providers by spend. Requires a session. Alias `cost provider`.

### Dimensions and filters

Pass these flags to `costgraph cost get`.

**--group-by** `string[]`

Sets dimension order. Separate dimensions with commas, such as `provider,service`.

**--filter** `string[]`

Filters `dimension:value`. Repeat a dimension for OR. Different dimensions narrow results together.

**--provider** `string`

Filters the provider without changing grouping.

**--sub-accounts** `boolean`

Breaks groups down by sub-account when attribution data exists.

**--layer** `string` (default: billed)

Chooses `billed` or `allocation` costs.

### Periods and dates

These flags control spend query windows.

**--period** `string` (default: month\_to\_date)

Selects `month_to_date`, `last_7d`, `last_30d`, `calendar_month`, or `custom`.

**--month** `string`

Sets the calendar month as `YYYY-MM`.

**--start, --end** `string`

Set custom date or RFC 3339 timestamp boundaries.

### Result order and output

Use these flags to control the returned rows.

**--sort** `string`

Sorts by `current`, `previous`, `change`, `share`, `usage`, or `name`. Prefix with `-` for ascending order.

**--limit** `integer`

Limits rows printed by the CLI.

**--group-limit** `integer`

Limits groups at each query level and combines remaining groups into `other`.

**--no-interactive** `boolean`

Prints a static result instead of opening the cost browser.

**-o, --output** `string`

Use `json` for structured totals, groups, and series.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.