Skip to content

Add --fields flag for context-window-friendly JSON output #347

@BYK

Description

@BYK

Summary

Add a --fields flag to commands with --json output, allowing agents (and humans) to request only specific fields in the response. This reduces token consumption and keeps agent context windows focused.

Motivation

From You Need to Rewrite Your CLI for AI Agents (Justin Poehnelt, Google DevRel):

"APIs return massive blobs. A single Gmail message can consume a meaningful fraction of an agent's context window. Humans don't care — humans scroll. Agents pay per token and lose reasoning capacity with every irrelevant field."

The same applies to Sentry CLI. A full sentry issue view --json dumps the entire issue object, latest event (with all exception entries, breadcrumbs, contexts, SDK metadata), and span tree. An agent that just needs id, title, and status is forced to ingest thousands of tokens of irrelevant data.

Current behavior

sentry issue list --json   # dumps full issue objects with all fields
sentry issue view --json   # dumps issue + event + trace — can be very large
sentry event view --json   # dumps full event with all entries

No way to limit which fields appear in JSON output.

Proposed behavior

# Select specific fields
sentry issue list --json --fields id,title,status,count
sentry issue view --json --fields id,title,metadata.value

# Works with nested fields using dot notation
sentry event view --json --fields eventID,dateCreated,contexts.trace

Design options

Option A: --fields flag

A simple comma-separated list of top-level (and optionally dot-notated nested) field names. Output only includes the requested fields.

sentry issue list --json --fields id,shortId,title,status

Pros: Simple, no external dependencies, easy to implement.
Cons: Limited expressiveness for complex filtering.

Option B: --jq flag (like gh --jq)

Pass output through a jq expression for arbitrary transformation.

sentry issue list --json --jq ".[].{id,title,status}"

Pros: Extremely powerful, familiar to CLI users.
Cons: Requires bundling or depending on jq, harder for agents to construct.

Option C: Both

Ship --fields for the common case, --jq for power users. Can be incremental.

Recommendation

Start with Option A (--fields). It covers the 90% case for agents (reduce output to only needed fields), is trivial to implement (filter object keys before JSON.stringify), and requires no external dependencies.

Implementation could live in src/lib/formatters/json.ts as a wrapper around writeJson:

function filterFields<T>(data: T, fields: string[]): Partial<T> {
  // Pick only the specified top-level keys (and dot-notated nested keys)
}

The --fields flag should only be valid when --json is also set.

Context

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions