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

# ubx optimize

> AI-powered cost optimisation — identify fixable and advisory savings in your XCL code.

`ubx optimize` reads `.xcl` files and recent cost history, then uses AI to identify concrete cost-saving opportunities. Recommendations are classified as `FIX` (auto-applicable) or `ADV` (advisory).

## Usage

```bash theme={"theme":"css-variables","languages":{"custom":["/languages/xcl.json"]}}
ubx optimize [flags]
```

## Flags

| Flag                    | Description                                                    |
| ----------------------- | -------------------------------------------------------------- |
| `--apply`               | Interactively apply fixable (`FIX`) recommendations            |
| `--format <fmt>`        | Output format: `text`, `json`                                  |
| `--min-saving <amount>` | Only show recommendations saving ≥ this amount per month (USD) |

## Requirements

Requires `UBX_AI_API_KEY` or `ai { enabled = true }` in workspace config.

## Examples

```bash theme={"theme":"css-variables","languages":{"custom":["/languages/xcl.json"]}}
ubx optimize                          # show all recommendations
ubx optimize --min-saving 10          # only savings >= $10/mo
ubx optimize --apply                  # interactively apply fixes
ubx optimize --format json            # JSON output for CI/dashboards
```

## Output

```
  FIX    networking/stack.xcl:14  aws.ec2.Instance  Downsize t3.large → t3.micro  (-$52.00/mo)
  ADV    —                        aws.rds.Instance  Purchase Reserved Instance     (-$120.00/mo)
  ADV    —                        aws.ec2.Instance  Switch to Spot for batch jobs  (-$35.00/mo)

  ─── Summary ──────────────────────────────────────────────
  1 fixable · 2 advisory
  Potential savings: -$207.00/mo  (fixable: -$52.00/mo · advisory: -$155.00/mo)
  Run 'ubx optimize --apply' to apply fixable recommendations.
```

## Recommendation types

| Type  | Description                                                                         |
| ----- | ----------------------------------------------------------------------------------- |
| `FIX` | Modifies a `.xcl` attribute in-place — instance size, feature toggles, storage tier |
| `ADV` | Advisory action in the cloud console — Reserved Instances, Spot, CDN, etc.          |

## Interactive apply

```bash theme={"theme":"css-variables","languages":{"custom":["/languages/xcl.json"]}}
ubx optimize --apply
```

For each `FIX` recommendation:

```
  FIX  networking/stack.xcl:14 — t3.large → t3.micro  (-$52.00/mo)
  Apply this fix? [y/N/s(kip all)]
```

* `y` — apply the fix (rewrites the `.xcl` file)
* `N` — skip this recommendation
* `s` — skip all remaining recommendations

After applying, ubx runs `ubx fmt` on modified files.

## JSON output

```json theme={"theme":"css-variables","languages":{"custom":["/languages/xcl.json"]}}
{
  "recommendations": [
    {
      "type": "FIX",
      "file": "networking/stack.xcl",
      "line": 14,
      "resource": "aws.ec2.Instance",
      "description": "Downsize instance from t3.large to t3.micro",
      "from": "t3.large",
      "to": "t3.micro",
      "saving": 52.00
    }
  ]
}
```

## AI model

Uses `configPkg.DefaultAIModel` (`claude-haiku-4-5`). Override in workspace config.
