> ## 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.

# Work with virtual tags in the CostGraph CLI

> List virtual tag rules, find fields to match, and preview which costs a new rule would tag

Inspect your tenant's virtual tags from the terminal, and check what a new rule would match before anyone saves it. A **virtual tag** assigns a tag value to cost rows that match a rule, without changing the tags on your infrastructure. Refer to [Virtual tags overview](https://docs.costgraph.ai/costgraph/virtual-tags/overview.md) for how rules are evaluated.

## Requirements

The `costgraph tags` commands need a signed-in CLI session. They don't accept API keys, including keys with `virtual-tags:read` or `virtual-tags:write`. Those scopes apply to the virtual tag management API that the Terraform provider uses. Refer to [Install and sign in](https://docs.costgraph.ai/costgraph/cli/install.md).

The commands read and preview rules in your session's tenant. Run `costgraph auth status` to check which tenant that is.

> [!NOTE]
>
> In CLI 0.11.0, `costgraph tags create` can validate and preview a rule but can't save it. CostGraph refuses the save request for both a saved CLI session and an API key. Save rules in the dashboard under **Settings > Tenants Settings > Virtual tags**, as described in [Create virtual tags](https://docs.costgraph.ai/costgraph/virtual-tags/usage.md), or with the Terraform provider.

## List tag keys and rules

The following command lists every tag key and its rules in evaluation order.

```shell theme={null}
costgraph tags list
```

The table shows each rule's key, rank, type, predicate, and assigned value. Long predicates are shortened to fit your terminal. Add `--wide` to print them in full, or `--key Team` to show one key.

### Browse rules interactively

Open the explorer with one key expanded.

```shell theme={null}
costgraph tags view Team
```

Use `/` to filter rules, Esc to clear the filter, and `q` to quit. Omit `Team` to browse every key.

Running `costgraph tags` without a subcommand opens this view in a terminal and prints help otherwise.

### Inspect one rule

`costgraph tags list --pick` lets you choose one key and rule in your terminal and prints that rule in detail.

## Find fields to match

A rule condition matches a field of your billing rows. The following command lists the fields you can use and how many rows populate each one.

```shell theme={null}
costgraph tags fields
```

The table shows each field with its coverage, populated rows, total rows, and whether it's usable. The CLI hides fields below the usability threshold. Add `--all` to include them. The list covers billing rows. It doesn't change for rules with the `k8s` scope.

## Preview a virtual tag rule

`costgraph tags create --dry-run` validates a rule and shows which cost rows it would match. With `--dry-run`, the command doesn't save the rule.

The following command previews a rule that assigns `Platform` to Amazon EC2 charges under the `Team` key.

```shell theme={null}
costgraph tags create --dry-run \
  --key Team \
  --field service_name --op equals --match AmazonEC2 \
  --value Platform
```

The output summarizes the rule, then the preview. The preview reports the number of matched rows, their spend in each currency, and a sample of the matched rows. Check that the matched spend is what you expect before you save the rule.

A rule creates no cost data, usage, or charges. It also applies to matching cost data that arrives later. The preview reflects only rows already imported into the tenant, so it can show zero matched rows for a rule that matches future data.

### Write the condition

Define the condition in one of two ways. Pass exactly one of them.

* A single clause uses `--field`, `--op`, and `--match`. `--op` defaults to `equals`. It also accepts `not_equals`, `contains`, `not_contains`, `starts_with`, and `ends_with`.
* A CostQL expression uses `--expr`, such as `--expr 'bill.service_name == "AmazonEC2"'`. Use an expression for conditions that combine several fields. Billing expressions qualify fields with `bill.`.

### Choose the rule type and scope

Choose how the rule obtains its value.

| Rule type | Value source |
| - | - |
| `literal` | Uses the fixed value in `--value`. This is the default. |
| `inherited_value` | Copies the expression in `--inherit-from`. Takes no `--field`, `--match`, or `--expr`. |

`--scope` chooses which cost data the rule applies to. `billing`, the default, matches billing line items. `k8s` matches Kubernetes allocation. This `--scope` is unrelated to the scopes on an [API key](https://docs.costgraph.ai/costgraph/cli/api-keys.md#api-key-scopes-and-virtual-tag-rule-scopes).

### Preview in a script

Add `-o json` to get the rule request and preview as JSON. Keep `--dry-run` in every scripted call.

> [!WARNING]
>
> Without `--dry-run`, `costgraph tags create` doesn't ask for confirmation when it runs in a script, a pipe, or with `-o json`. It sends the write request to CostGraph immediately. In CLI 0.11.0, CostGraph refuses that request, as described in [Requirements](#requirements). Pass `--dry-run` whenever you only want a preview.

```shell theme={null}
costgraph tags create --dry-run -o json \
  --key Team \
  --field service_name \
  --match AmazonEC2 \
  --value Platform
```

## Group costs by a virtual tag

After a rule is saved and applied, group costs by its key with `tag:<key>`.

```shell theme={null}
costgraph cost get --group-by tag:Team --no-interactive
```

Refer to [Query costs](https://docs.costgraph.ai/costgraph/cli/query-costs.md) for filters and periods.

## Resolve virtual tag errors

The following messages come from `costgraph tags` commands:

| Message | Cause and remedy |
| - | - |
| `Valid Bearer token required (401)` | The CLI sent an API key. Run `costgraph auth login`, then run the command again. |
| `This application is not allowed to access this resource (403)` | `costgraph tags create` tried to save a rule. CLI 0.11.0 can't save rules. Save the rule in the dashboard or with Terraform. |
| `pass either --expr or --field/--match, not both` | Use one way to write the condition. |
| `--field and --match go together` | Add the missing `--field` or `--match`. |
| `--value is required` | Add `--value` to a `literal` rule. |
| `--inherit-from is required for inherited_value rules` | Add `--inherit-from`, or use `--rule-type literal`. |

## Command reference

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

### Browse and inspect

All `tags` commands require a saved session. `tag` is an alias for `tags`.

**tags list** `command`

Lists keys and rules. Use `--key` for one key, `--wide` for full predicates, or `--pick` to inspect a rule.

**tags view \[key]** `command`

Opens the interactive explorer. Bare `tags` opens it in a terminal and prints help otherwise.

**tags fields** `command`

Lists billing fields and coverage. Use `--all` to include fields below the usability threshold.

### Rule preview

CLI 0.11.0 can preview rules but cannot save them.

**tags create** `command`

Validates and previews a rule with `--dry-run`. Keep that flag for previews in scripts.

### Rule condition flags

Use a simple clause or a CostQL expression for a literal rule.

**--key** `string`

Names the virtual tag key.

**--field, --match** `string`

Set the billing field and match value for a simple clause.

**--op** `string` (default: equals)

Chooses `equals`, `not_equals`, `contains`, `not_contains`, `starts_with`, or `ends_with`.

**--expr** `string`

Sets a CostQL condition instead of `--field` and `--match`. Qualify billing fields with `bill.`.

**--rule-type** `string` (default: literal)

Chooses `literal` or `inherited_value`.

**--value** `string`

Sets the fixed tag value for a literal rule.

**--inherit-from** `string`

Sets the value expression for an inherited rule.

### Scope and output flags

These flags select the data and preview behavior.

**--scope** `string` (default: billing)

Selects `billing` or `k8s` cost data. Does not grant API key permissions.

**--dry-run** `boolean`

Previews matching rows without saving a rule.

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

Use `json` for the rule request and preview. Keep `--dry-run`.


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