/ utils / privacyLevel.ts
privacyLevel.ts
 1  /**
 2   * Privacy level controls how much nonessential network traffic and telemetry
 3   * Claude Code generates.
 4   *
 5   * Levels are ordered by restrictiveness:
 6   *   default < no-telemetry < essential-traffic
 7   *
 8   * - default:            Everything enabled.
 9   * - no-telemetry:       Analytics/telemetry disabled (Datadog, 1P events, feedback survey).
10   * - essential-traffic:  ALL nonessential network traffic disabled
11   *                       (telemetry + auto-updates, grove, release notes, model capabilities, etc.).
12   *
13   * The resolved level is the most restrictive signal from:
14   *   CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC  →  essential-traffic
15   *   DISABLE_TELEMETRY                         →  no-telemetry
16   */
17  
18  type PrivacyLevel = 'default' | 'no-telemetry' | 'essential-traffic'
19  
20  export function getPrivacyLevel(): PrivacyLevel {
21    if (process.env.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC) {
22      return 'essential-traffic'
23    }
24    if (process.env.DISABLE_TELEMETRY) {
25      return 'no-telemetry'
26    }
27    return 'default'
28  }
29  
30  /**
31   * True when all nonessential network traffic should be suppressed.
32   * Equivalent to the old `process.env.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC` check.
33   */
34  export function isEssentialTrafficOnly(): boolean {
35    return getPrivacyLevel() === 'essential-traffic'
36  }
37  
38  /**
39   * True when telemetry/analytics should be suppressed.
40   * True at both `no-telemetry` and `essential-traffic` levels.
41   */
42  export function isTelemetryDisabled(): boolean {
43    return getPrivacyLevel() !== 'default'
44  }
45  
46  /**
47   * Returns the env var name responsible for the current essential-traffic restriction,
48   * or null if unrestricted. Used for user-facing "unset X to re-enable" messages.
49   */
50  export function getEssentialTrafficOnlyReason(): string | null {
51    if (process.env.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC) {
52      return 'CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC'
53    }
54    return null
55  }