@evolvingmachines/sdk 0.0.27 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -581,6 +581,8 @@ interface SessionStatus {
581
581
  interface AgentResponse {
582
582
  /** Sandbox ID for session management */
583
583
  sandboxId: string;
584
+ /** Run ID for spend/cost attribution (present for run(), undefined for executeCommand()) */
585
+ runId?: string;
584
586
  /** Exit code of the command */
585
587
  exitCode: number;
586
588
  /** Standard output */
@@ -590,6 +592,50 @@ interface AgentResponse {
590
592
  /** Checkpoint info if storage configured and run succeeded (undefined otherwise) */
591
593
  checkpoint?: CheckpointInfo;
592
594
  }
595
+ /** Cost breakdown for a single run() invocation */
596
+ interface RunCost {
597
+ /** Run ID matching AgentResponse.runId */
598
+ runId: string;
599
+ /** 1-based chronological position in session */
600
+ index: number;
601
+ /** Total cost in USD (includes platform margin) */
602
+ cost: number;
603
+ /** Token counts */
604
+ tokens: {
605
+ prompt: number;
606
+ completion: number;
607
+ };
608
+ /** Model used (e.g., "claude-opus-4-6"). Last observed model if multiple models used in a run. */
609
+ model: string;
610
+ /** Number of LLM API requests in this run */
611
+ requests: number;
612
+ /** ISO timestamp when this data was fetched */
613
+ asOf: string;
614
+ /** False if recent LLM calls may still be batching (~60s delay) */
615
+ isComplete: boolean;
616
+ /** True if spend log pagination was capped — totals may be understated */
617
+ truncated: boolean;
618
+ }
619
+ /** Cost breakdown for an entire agent session (all runs) */
620
+ interface SessionCost {
621
+ /** Session tag matching agent.getSessionTag() */
622
+ sessionTag: string;
623
+ /** Total cost across all runs in USD */
624
+ totalCost: number;
625
+ /** Aggregate token counts */
626
+ totalTokens: {
627
+ prompt: number;
628
+ completion: number;
629
+ };
630
+ /** Per-run breakdown, chronological order */
631
+ runs: RunCost[];
632
+ /** ISO timestamp when this data was fetched */
633
+ asOf: string;
634
+ /** False if session is still active or recently ended */
635
+ isComplete: boolean;
636
+ /** True if spend log pagination was capped — totals may be understated */
637
+ truncated: boolean;
638
+ }
593
639
  /** Result from getOutputFiles() with optional schema validation */
594
640
  interface OutputResult<T = unknown> {
595
641
  /** Output files from output/ folder */
@@ -747,6 +793,43 @@ interface CheckpointInfo {
747
793
  /** User-provided label for this checkpoint */
748
794
  comment?: string;
749
795
  }
796
+ /** Options for StorageClient.downloadCheckpoint() */
797
+ interface DownloadCheckpointOptions {
798
+ /** Local directory to save to (default: current working directory) */
799
+ to?: string;
800
+ /** Extract the archive (default: true). If false, saves the raw .tar.gz file. */
801
+ extract?: boolean;
802
+ }
803
+ /** Options for StorageClient.downloadFiles() */
804
+ interface DownloadFilesOptions {
805
+ /** Specific file paths to extract (relative to archive root, e.g., "workspace/output/result.json") */
806
+ files?: string[];
807
+ /** Glob patterns to match files (e.g., ["workspace/output/*.json"]) */
808
+ glob?: string[];
809
+ /** Local directory to save files to. If omitted, files are returned in-memory only. */
810
+ to?: string;
811
+ }
812
+ /**
813
+ * Storage client for browsing and fetching checkpoints without an Evolve instance.
814
+ *
815
+ * @example
816
+ * const s = storage({ url: "s3://my-bucket/prefix/" });
817
+ * const checkpoints = await s.listCheckpoints({ tag: "poker-agent" });
818
+ * const files = await s.downloadFiles("latest", { glob: ["workspace/output/*.json"] });
819
+ */
820
+ interface StorageClient {
821
+ /** List checkpoints with optional filtering */
822
+ listCheckpoints(options?: {
823
+ limit?: number;
824
+ tag?: string;
825
+ }): Promise<CheckpointInfo[]>;
826
+ /** Get a specific checkpoint's metadata by ID */
827
+ getCheckpoint(id: string): Promise<CheckpointInfo>;
828
+ /** Download an entire checkpoint archive. Returns the output path. */
829
+ downloadCheckpoint(idOrLatest: string, options?: DownloadCheckpointOptions): Promise<string>;
830
+ /** Download files from a checkpoint as a FileMap. */
831
+ downloadFiles(idOrLatest: string, options?: DownloadFilesOptions): Promise<FileMap>;
832
+ }
750
833
 
751
834
  /**
752
835
  * Composio Auth Helpers
@@ -845,6 +928,10 @@ declare class Agent {
845
928
  private readonly workingDir;
846
929
  private lastRunTimestamp?;
847
930
  private readonly registry;
931
+ /** Unified session ID — used for both observability (SessionLogger) and spend tracking (LiteLLM customer-id) */
932
+ private sessionTag;
933
+ /** Previous session tag — preserved across kill()/setSession() so cost queries still work */
934
+ private previousSessionTag?;
848
935
  private sessionLogger?;
849
936
  private activeCommand?;
850
937
  private activeProcessId;
@@ -879,6 +966,13 @@ declare class Agent {
879
966
  * Build environment variables for sandbox
880
967
  */
881
968
  private buildEnvironmentVariables;
969
+ /**
970
+ * Build per-run env overrides for spend tracking.
971
+ * Merges session + run headers into the custom headers env var,
972
+ * or sets per-env-var values for agents using spendTrackingEnvs.
973
+ * Passed to spawn() so each .run() gets a unique run tag.
974
+ */
975
+ private buildRunEnvs;
882
976
  /**
883
977
  * Agent-specific authentication setup
884
978
  */
@@ -987,9 +1081,9 @@ declare class Agent {
987
1081
  */
988
1082
  getAgentType(): AgentType;
989
1083
  /**
990
- * Get current session tag
991
- *
992
- * Returns null if no session has started (run() not called yet).
1084
+ * Get current session tag.
1085
+ * Returns null if no active session (before sandbox creation or after kill()).
1086
+ * Used for both observability (dashboard traces) and spend tracking (LiteLLM customer-id).
993
1087
  */
994
1088
  getSessionTag(): string | null;
995
1089
  /**
@@ -998,6 +1092,62 @@ declare class Agent {
998
1092
  * Returns null if no session has started (run() not called yet).
999
1093
  */
1000
1094
  getSessionTimestamp(): string | null;
1095
+ /**
1096
+ * Flush pending observability events without closing the session.
1097
+ */
1098
+ flushObservability(): Promise<void>;
1099
+ /**
1100
+ * Tear down the session logger and rotate the session tag.
1101
+ * Preserves `previousSessionTag` only if this session had actual activity,
1102
+ * so a double kill() or no-op lifecycle call doesn't clobber the real tag.
1103
+ * @internal
1104
+ */
1105
+ private rotateSession;
1106
+ /**
1107
+ * Fetch spend data from dashboard API.
1108
+ * @internal
1109
+ */
1110
+ private fetchSpend;
1111
+ /**
1112
+ * Resolve the session tag for cost queries.
1113
+ * Uses the active session tag, or falls back to the previous tag after kill()/setSession().
1114
+ * @internal
1115
+ */
1116
+ private resolveSpendTag;
1117
+ /**
1118
+ * Normalize run payloads for compatibility with older dashboard responses.
1119
+ * Older responses may omit `asOf`, `isComplete`, or `truncated` inside `runs[]`.
1120
+ * @internal
1121
+ */
1122
+ private normalizeRunCost;
1123
+ /**
1124
+ * Normalize session payloads so all `runs[]` conform to `RunCost`.
1125
+ * @internal
1126
+ */
1127
+ private normalizeSessionCost;
1128
+ /**
1129
+ * Get cost breakdown for the current session (all runs).
1130
+ *
1131
+ * Queries the dashboard API which proxies to LiteLLM spend logs.
1132
+ * Cost data has ~60s latency due to gateway batch writes.
1133
+ * Also works after kill() for the most recent session only.
1134
+ *
1135
+ * Requires gateway mode (EVOLVE_API_KEY).
1136
+ */
1137
+ getSessionCost(): Promise<SessionCost>;
1138
+ /**
1139
+ * Get cost for a specific run by ID or index.
1140
+ *
1141
+ * @param run - Either `{ runId: string }` or `{ index: number }` (1-based, negative = from end)
1142
+ *
1143
+ * Also works after kill() for the most recent session only.
1144
+ * Requires gateway mode (EVOLVE_API_KEY).
1145
+ */
1146
+ getRunCost(run: {
1147
+ runId: string;
1148
+ } | {
1149
+ index: number;
1150
+ }): Promise<RunCost>;
1001
1151
  }
1002
1152
 
1003
1153
  /**
@@ -1415,6 +1565,12 @@ declare class Evolve extends EventEmitter {
1415
1565
  checkpoint(options?: {
1416
1566
  comment?: string;
1417
1567
  }): Promise<CheckpointInfo>;
1568
+ private _cachedGatewayOverrides;
1569
+ /**
1570
+ * Resolve gateway credentials from agent config for storage operations.
1571
+ * Memoized — agent config is immutable after .withAgent().
1572
+ */
1573
+ private resolveGatewayOverrides;
1418
1574
  /**
1419
1575
  * List checkpoints (requires .withStorage()).
1420
1576
  *
@@ -1427,6 +1583,11 @@ declare class Evolve extends EventEmitter {
1427
1583
  limit?: number;
1428
1584
  tag?: string;
1429
1585
  }): Promise<CheckpointInfo[]>;
1586
+ /**
1587
+ * Get a StorageClient bound to this instance's storage configuration.
1588
+ * Same API surface as the standalone storage() factory.
1589
+ */
1590
+ storage(): StorageClient;
1430
1591
  /**
1431
1592
  * Get current session (sandbox ID)
1432
1593
  */
@@ -1467,6 +1628,25 @@ declare class Evolve extends EventEmitter {
1467
1628
  * Returns null if no session has started (run() not called yet).
1468
1629
  */
1469
1630
  getSessionTimestamp(): string | null;
1631
+ /**
1632
+ * Flush pending observability events without killing sandbox.
1633
+ */
1634
+ flushObservability(): Promise<void>;
1635
+ /**
1636
+ * Get cost breakdown for the current session (all runs).
1637
+ * Also works after kill() — queries the just-finished session.
1638
+ * Requires gateway mode (EVOLVE_API_KEY).
1639
+ */
1640
+ getSessionCost(): Promise<SessionCost>;
1641
+ /**
1642
+ * Get cost for a specific run by ID or index.
1643
+ * @param run - Either `{ runId: string }` or `{ index: number }` (1-based, negative = from end)
1644
+ */
1645
+ getRunCost(run: {
1646
+ runId: string;
1647
+ } | {
1648
+ index: number;
1649
+ }): Promise<RunCost>;
1470
1650
  }
1471
1651
 
1472
1652
  /**
@@ -2384,6 +2564,20 @@ interface AgentRegistryEntry {
2384
2564
  }>;
2385
2565
  /** Env var for inline config (e.g., OPENCODE_CONFIG_CONTENT) — used in gateway mode to set provider base URLs */
2386
2566
  gatewayConfigEnv?: string;
2567
+ /** Environment variable that CLI reads for custom outbound HTTP headers */
2568
+ customHeadersEnv?: string;
2569
+ /**
2570
+ * Per-env-var spend tracking for CLIs that support env_http_headers in config
2571
+ * (e.g., Codex TOML). Maps LiteLLM header names to env var names that the CLI
2572
+ * reads at request time. Alternative to customHeadersEnv for agents without a
2573
+ * single custom-headers env var.
2574
+ */
2575
+ spendTrackingEnvs?: {
2576
+ /** Env var name for x-litellm-customer-id value */
2577
+ sessionTagEnv: string;
2578
+ /** Env var name for x-litellm-tags value */
2579
+ runTagEnv: string;
2580
+ };
2387
2581
  /** Additional directories to include in checkpoint tar (beyond mcpConfig.settingsDir).
2388
2582
  * Used for agents like OpenCode that spread state across XDG directories. */
2389
2583
  checkpointDirs?: string[];
@@ -2628,22 +2822,6 @@ declare function saveLocalDir(localPath: string, files: FileMap): void;
2628
2822
  * Gateway mode: no URL → use dashboard API endpoints
2629
2823
  */
2630
2824
  declare function resolveStorageConfig(config: StorageConfig | undefined, isGateway: boolean, gatewayUrl?: string, gatewayApiKey?: string): ResolvedStorageConfig;
2631
- /**
2632
- * List checkpoints (standalone — no Evolve instance needed).
2633
- *
2634
- * BYOK mode: reads directly from S3.
2635
- * Gateway mode: reads EVOLVE_API_KEY from env, calls dashboard API.
2636
- *
2637
- * @example
2638
- * // BYOK
2639
- * const all = await listCheckpoints({ url: "s3://my-bucket/project/" });
2640
- *
2641
- * // Gateway
2642
- * const all = await listCheckpoints({});
2643
- */
2644
- declare function listCheckpoints(config: StorageConfig, options?: {
2645
- limit?: number;
2646
- tag?: string;
2647
- }): Promise<CheckpointInfo[]>;
2825
+ declare function storage(config?: StorageConfig): StorageClient;
2648
2826
 
2649
- export { AGENT_REGISTRY, AGENT_TYPES, Agent, type AgentConfig, type AgentOptions, type AgentOverride, type AgentParser, type AgentRegistryEntry, type AgentResponse, type AgentRuntimeState, type AgentType, type BaseMeta, type BestOfConfig, type BestOfParams, type BestOfResult, type CandidateCompleteEvent, type CheckpointInfo, type ComposioAuthResult, type ComposioConfig, type ComposioConnectionStatus, type ComposioSetup, type EmitOption, type EventHandler, type EventName, Evolve, type EvolveConfig, type EvolveEvents, type ExecuteCommandOptions, type FileMap, type FilterConfig, type FilterParams, type IndexedMeta, type ItemInput, type ItemRetryEvent, JUDGE_PROMPT, type JsonSchema, type JudgeCompleteEvent, type JudgeDecision, type JudgeMeta, type LifecycleEvent, type LifecycleReason, type MapConfig, type MapParams, type McpConfigInfo, type McpServerConfig, type ModelInfo, type OnCandidateCompleteCallback, type OnItemRetryCallback, type OnJudgeCompleteCallback, type OnVerifierCompleteCallback, type OnWorkerCompleteCallback, type OperationType, type OutputEvent, type OutputResult, Pipeline, type PipelineContext, type PipelineEventMap, type PipelineEvents, type PipelineResult, type ProcessInfo, type Prompt, type PromptFn, RETRY_FEEDBACK_PROMPT, type ReasoningEffort, type ReduceConfig, type ReduceMeta, type ReduceParams, type ReduceResult, type ResolvedStorageConfig, type RetryConfig, type RunOptions, SCHEMA_PROMPT, SWARM_RESULT_BRAND, SYSTEM_PROMPT, type SandboxCommandHandle, type SandboxCommandResult, type SandboxCommands, type SandboxCreateOptions, type SandboxFiles, type SandboxInstance, type SandboxLifecycleState, type SandboxProvider, type SandboxRunOptions, type SandboxSpawnOptions, type SchemaValidationOptions, Semaphore, type SessionStatus, type SkillName, type SkillsConfig, type StepCompleteEvent, type StepErrorEvent, type StepEvent, type StepResult, type StepStartEvent, type StorageConfig, type StreamCallbacks, Swarm, type SwarmConfig, type SwarmResult, SwarmResultList, TerminalPipeline, type ToolsFilter, VALIDATION_PRESETS, VERIFY_PROMPT, type ValidationMode, type VerifierCompleteEvent, type VerifyConfig, type VerifyDecision, type VerifyInfo, type VerifyMeta, WORKSPACE_PROMPT, WORKSPACE_SWE_PROMPT, type WorkerCompleteEvent, type WorkspaceMode, applyTemplate, buildWorkerSystemPrompt, createAgentParser, createClaudeParser, createCodexParser, createGeminiParser, executeWithRetry, expandPath, getAgentConfig, getMcpSettingsDir, getMcpSettingsPath, isValidAgentType, isZodSchema, jsonSchemaToString, listCheckpoints, parseNdjsonLine, parseNdjsonOutput, parseQwenOutput, readLocalDir, resolveStorageConfig, saveLocalDir, writeClaudeMcpConfig, writeCodexMcpConfig, writeGeminiMcpConfig, writeMcpConfig, writeQwenMcpConfig, zodSchemaToJson };
2827
+ export { AGENT_REGISTRY, AGENT_TYPES, Agent, type AgentConfig, type AgentOptions, type AgentOverride, type AgentParser, type AgentRegistryEntry, type AgentResponse, type AgentRuntimeState, type AgentType, type BaseMeta, type BestOfConfig, type BestOfParams, type BestOfResult, type CandidateCompleteEvent, type CheckpointInfo, type ComposioAuthResult, type ComposioConfig, type ComposioConnectionStatus, type ComposioSetup, type DownloadCheckpointOptions, type DownloadFilesOptions, type EmitOption, type EventHandler, type EventName, Evolve, type EvolveConfig, type EvolveEvents, type ExecuteCommandOptions, type FileMap, type FilterConfig, type FilterParams, type IndexedMeta, type ItemInput, type ItemRetryEvent, JUDGE_PROMPT, type JsonSchema, type JudgeCompleteEvent, type JudgeDecision, type JudgeMeta, type LifecycleEvent, type LifecycleReason, type MapConfig, type MapParams, type McpConfigInfo, type McpServerConfig, type ModelInfo, type OnCandidateCompleteCallback, type OnItemRetryCallback, type OnJudgeCompleteCallback, type OnVerifierCompleteCallback, type OnWorkerCompleteCallback, type OperationType, type OutputEvent, type OutputResult, Pipeline, type PipelineContext, type PipelineEventMap, type PipelineEvents, type PipelineResult, type ProcessInfo, type Prompt, type PromptFn, RETRY_FEEDBACK_PROMPT, type ReasoningEffort, type ReduceConfig, type ReduceMeta, type ReduceParams, type ReduceResult, type ResolvedStorageConfig, type RetryConfig, type RunCost, type RunOptions, SCHEMA_PROMPT, SWARM_RESULT_BRAND, SYSTEM_PROMPT, type SandboxCommandHandle, type SandboxCommandResult, type SandboxCommands, type SandboxCreateOptions, type SandboxFiles, type SandboxInstance, type SandboxLifecycleState, type SandboxProvider, type SandboxRunOptions, type SandboxSpawnOptions, type SchemaValidationOptions, Semaphore, type SessionCost, type SessionStatus, type SkillName, type SkillsConfig, type StepCompleteEvent, type StepErrorEvent, type StepEvent, type StepResult, type StepStartEvent, type StorageClient, type StorageConfig, type StreamCallbacks, Swarm, type SwarmConfig, type SwarmResult, SwarmResultList, TerminalPipeline, type ToolsFilter, VALIDATION_PRESETS, VERIFY_PROMPT, type ValidationMode, type VerifierCompleteEvent, type VerifyConfig, type VerifyDecision, type VerifyInfo, type VerifyMeta, WORKSPACE_PROMPT, WORKSPACE_SWE_PROMPT, type WorkerCompleteEvent, type WorkspaceMode, applyTemplate, buildWorkerSystemPrompt, createAgentParser, createClaudeParser, createCodexParser, createGeminiParser, executeWithRetry, expandPath, getAgentConfig, getMcpSettingsDir, getMcpSettingsPath, isValidAgentType, isZodSchema, jsonSchemaToString, parseNdjsonLine, parseNdjsonOutput, parseQwenOutput, readLocalDir, resolveStorageConfig, saveLocalDir, storage, writeClaudeMcpConfig, writeCodexMcpConfig, writeGeminiMcpConfig, writeMcpConfig, writeQwenMcpConfig, zodSchemaToJson };
package/dist/index.d.ts CHANGED
@@ -581,6 +581,8 @@ interface SessionStatus {
581
581
  interface AgentResponse {
582
582
  /** Sandbox ID for session management */
583
583
  sandboxId: string;
584
+ /** Run ID for spend/cost attribution (present for run(), undefined for executeCommand()) */
585
+ runId?: string;
584
586
  /** Exit code of the command */
585
587
  exitCode: number;
586
588
  /** Standard output */
@@ -590,6 +592,50 @@ interface AgentResponse {
590
592
  /** Checkpoint info if storage configured and run succeeded (undefined otherwise) */
591
593
  checkpoint?: CheckpointInfo;
592
594
  }
595
+ /** Cost breakdown for a single run() invocation */
596
+ interface RunCost {
597
+ /** Run ID matching AgentResponse.runId */
598
+ runId: string;
599
+ /** 1-based chronological position in session */
600
+ index: number;
601
+ /** Total cost in USD (includes platform margin) */
602
+ cost: number;
603
+ /** Token counts */
604
+ tokens: {
605
+ prompt: number;
606
+ completion: number;
607
+ };
608
+ /** Model used (e.g., "claude-opus-4-6"). Last observed model if multiple models used in a run. */
609
+ model: string;
610
+ /** Number of LLM API requests in this run */
611
+ requests: number;
612
+ /** ISO timestamp when this data was fetched */
613
+ asOf: string;
614
+ /** False if recent LLM calls may still be batching (~60s delay) */
615
+ isComplete: boolean;
616
+ /** True if spend log pagination was capped — totals may be understated */
617
+ truncated: boolean;
618
+ }
619
+ /** Cost breakdown for an entire agent session (all runs) */
620
+ interface SessionCost {
621
+ /** Session tag matching agent.getSessionTag() */
622
+ sessionTag: string;
623
+ /** Total cost across all runs in USD */
624
+ totalCost: number;
625
+ /** Aggregate token counts */
626
+ totalTokens: {
627
+ prompt: number;
628
+ completion: number;
629
+ };
630
+ /** Per-run breakdown, chronological order */
631
+ runs: RunCost[];
632
+ /** ISO timestamp when this data was fetched */
633
+ asOf: string;
634
+ /** False if session is still active or recently ended */
635
+ isComplete: boolean;
636
+ /** True if spend log pagination was capped — totals may be understated */
637
+ truncated: boolean;
638
+ }
593
639
  /** Result from getOutputFiles() with optional schema validation */
594
640
  interface OutputResult<T = unknown> {
595
641
  /** Output files from output/ folder */
@@ -747,6 +793,43 @@ interface CheckpointInfo {
747
793
  /** User-provided label for this checkpoint */
748
794
  comment?: string;
749
795
  }
796
+ /** Options for StorageClient.downloadCheckpoint() */
797
+ interface DownloadCheckpointOptions {
798
+ /** Local directory to save to (default: current working directory) */
799
+ to?: string;
800
+ /** Extract the archive (default: true). If false, saves the raw .tar.gz file. */
801
+ extract?: boolean;
802
+ }
803
+ /** Options for StorageClient.downloadFiles() */
804
+ interface DownloadFilesOptions {
805
+ /** Specific file paths to extract (relative to archive root, e.g., "workspace/output/result.json") */
806
+ files?: string[];
807
+ /** Glob patterns to match files (e.g., ["workspace/output/*.json"]) */
808
+ glob?: string[];
809
+ /** Local directory to save files to. If omitted, files are returned in-memory only. */
810
+ to?: string;
811
+ }
812
+ /**
813
+ * Storage client for browsing and fetching checkpoints without an Evolve instance.
814
+ *
815
+ * @example
816
+ * const s = storage({ url: "s3://my-bucket/prefix/" });
817
+ * const checkpoints = await s.listCheckpoints({ tag: "poker-agent" });
818
+ * const files = await s.downloadFiles("latest", { glob: ["workspace/output/*.json"] });
819
+ */
820
+ interface StorageClient {
821
+ /** List checkpoints with optional filtering */
822
+ listCheckpoints(options?: {
823
+ limit?: number;
824
+ tag?: string;
825
+ }): Promise<CheckpointInfo[]>;
826
+ /** Get a specific checkpoint's metadata by ID */
827
+ getCheckpoint(id: string): Promise<CheckpointInfo>;
828
+ /** Download an entire checkpoint archive. Returns the output path. */
829
+ downloadCheckpoint(idOrLatest: string, options?: DownloadCheckpointOptions): Promise<string>;
830
+ /** Download files from a checkpoint as a FileMap. */
831
+ downloadFiles(idOrLatest: string, options?: DownloadFilesOptions): Promise<FileMap>;
832
+ }
750
833
 
751
834
  /**
752
835
  * Composio Auth Helpers
@@ -845,6 +928,10 @@ declare class Agent {
845
928
  private readonly workingDir;
846
929
  private lastRunTimestamp?;
847
930
  private readonly registry;
931
+ /** Unified session ID — used for both observability (SessionLogger) and spend tracking (LiteLLM customer-id) */
932
+ private sessionTag;
933
+ /** Previous session tag — preserved across kill()/setSession() so cost queries still work */
934
+ private previousSessionTag?;
848
935
  private sessionLogger?;
849
936
  private activeCommand?;
850
937
  private activeProcessId;
@@ -879,6 +966,13 @@ declare class Agent {
879
966
  * Build environment variables for sandbox
880
967
  */
881
968
  private buildEnvironmentVariables;
969
+ /**
970
+ * Build per-run env overrides for spend tracking.
971
+ * Merges session + run headers into the custom headers env var,
972
+ * or sets per-env-var values for agents using spendTrackingEnvs.
973
+ * Passed to spawn() so each .run() gets a unique run tag.
974
+ */
975
+ private buildRunEnvs;
882
976
  /**
883
977
  * Agent-specific authentication setup
884
978
  */
@@ -987,9 +1081,9 @@ declare class Agent {
987
1081
  */
988
1082
  getAgentType(): AgentType;
989
1083
  /**
990
- * Get current session tag
991
- *
992
- * Returns null if no session has started (run() not called yet).
1084
+ * Get current session tag.
1085
+ * Returns null if no active session (before sandbox creation or after kill()).
1086
+ * Used for both observability (dashboard traces) and spend tracking (LiteLLM customer-id).
993
1087
  */
994
1088
  getSessionTag(): string | null;
995
1089
  /**
@@ -998,6 +1092,62 @@ declare class Agent {
998
1092
  * Returns null if no session has started (run() not called yet).
999
1093
  */
1000
1094
  getSessionTimestamp(): string | null;
1095
+ /**
1096
+ * Flush pending observability events without closing the session.
1097
+ */
1098
+ flushObservability(): Promise<void>;
1099
+ /**
1100
+ * Tear down the session logger and rotate the session tag.
1101
+ * Preserves `previousSessionTag` only if this session had actual activity,
1102
+ * so a double kill() or no-op lifecycle call doesn't clobber the real tag.
1103
+ * @internal
1104
+ */
1105
+ private rotateSession;
1106
+ /**
1107
+ * Fetch spend data from dashboard API.
1108
+ * @internal
1109
+ */
1110
+ private fetchSpend;
1111
+ /**
1112
+ * Resolve the session tag for cost queries.
1113
+ * Uses the active session tag, or falls back to the previous tag after kill()/setSession().
1114
+ * @internal
1115
+ */
1116
+ private resolveSpendTag;
1117
+ /**
1118
+ * Normalize run payloads for compatibility with older dashboard responses.
1119
+ * Older responses may omit `asOf`, `isComplete`, or `truncated` inside `runs[]`.
1120
+ * @internal
1121
+ */
1122
+ private normalizeRunCost;
1123
+ /**
1124
+ * Normalize session payloads so all `runs[]` conform to `RunCost`.
1125
+ * @internal
1126
+ */
1127
+ private normalizeSessionCost;
1128
+ /**
1129
+ * Get cost breakdown for the current session (all runs).
1130
+ *
1131
+ * Queries the dashboard API which proxies to LiteLLM spend logs.
1132
+ * Cost data has ~60s latency due to gateway batch writes.
1133
+ * Also works after kill() for the most recent session only.
1134
+ *
1135
+ * Requires gateway mode (EVOLVE_API_KEY).
1136
+ */
1137
+ getSessionCost(): Promise<SessionCost>;
1138
+ /**
1139
+ * Get cost for a specific run by ID or index.
1140
+ *
1141
+ * @param run - Either `{ runId: string }` or `{ index: number }` (1-based, negative = from end)
1142
+ *
1143
+ * Also works after kill() for the most recent session only.
1144
+ * Requires gateway mode (EVOLVE_API_KEY).
1145
+ */
1146
+ getRunCost(run: {
1147
+ runId: string;
1148
+ } | {
1149
+ index: number;
1150
+ }): Promise<RunCost>;
1001
1151
  }
1002
1152
 
1003
1153
  /**
@@ -1415,6 +1565,12 @@ declare class Evolve extends EventEmitter {
1415
1565
  checkpoint(options?: {
1416
1566
  comment?: string;
1417
1567
  }): Promise<CheckpointInfo>;
1568
+ private _cachedGatewayOverrides;
1569
+ /**
1570
+ * Resolve gateway credentials from agent config for storage operations.
1571
+ * Memoized — agent config is immutable after .withAgent().
1572
+ */
1573
+ private resolveGatewayOverrides;
1418
1574
  /**
1419
1575
  * List checkpoints (requires .withStorage()).
1420
1576
  *
@@ -1427,6 +1583,11 @@ declare class Evolve extends EventEmitter {
1427
1583
  limit?: number;
1428
1584
  tag?: string;
1429
1585
  }): Promise<CheckpointInfo[]>;
1586
+ /**
1587
+ * Get a StorageClient bound to this instance's storage configuration.
1588
+ * Same API surface as the standalone storage() factory.
1589
+ */
1590
+ storage(): StorageClient;
1430
1591
  /**
1431
1592
  * Get current session (sandbox ID)
1432
1593
  */
@@ -1467,6 +1628,25 @@ declare class Evolve extends EventEmitter {
1467
1628
  * Returns null if no session has started (run() not called yet).
1468
1629
  */
1469
1630
  getSessionTimestamp(): string | null;
1631
+ /**
1632
+ * Flush pending observability events without killing sandbox.
1633
+ */
1634
+ flushObservability(): Promise<void>;
1635
+ /**
1636
+ * Get cost breakdown for the current session (all runs).
1637
+ * Also works after kill() — queries the just-finished session.
1638
+ * Requires gateway mode (EVOLVE_API_KEY).
1639
+ */
1640
+ getSessionCost(): Promise<SessionCost>;
1641
+ /**
1642
+ * Get cost for a specific run by ID or index.
1643
+ * @param run - Either `{ runId: string }` or `{ index: number }` (1-based, negative = from end)
1644
+ */
1645
+ getRunCost(run: {
1646
+ runId: string;
1647
+ } | {
1648
+ index: number;
1649
+ }): Promise<RunCost>;
1470
1650
  }
1471
1651
 
1472
1652
  /**
@@ -2384,6 +2564,20 @@ interface AgentRegistryEntry {
2384
2564
  }>;
2385
2565
  /** Env var for inline config (e.g., OPENCODE_CONFIG_CONTENT) — used in gateway mode to set provider base URLs */
2386
2566
  gatewayConfigEnv?: string;
2567
+ /** Environment variable that CLI reads for custom outbound HTTP headers */
2568
+ customHeadersEnv?: string;
2569
+ /**
2570
+ * Per-env-var spend tracking for CLIs that support env_http_headers in config
2571
+ * (e.g., Codex TOML). Maps LiteLLM header names to env var names that the CLI
2572
+ * reads at request time. Alternative to customHeadersEnv for agents without a
2573
+ * single custom-headers env var.
2574
+ */
2575
+ spendTrackingEnvs?: {
2576
+ /** Env var name for x-litellm-customer-id value */
2577
+ sessionTagEnv: string;
2578
+ /** Env var name for x-litellm-tags value */
2579
+ runTagEnv: string;
2580
+ };
2387
2581
  /** Additional directories to include in checkpoint tar (beyond mcpConfig.settingsDir).
2388
2582
  * Used for agents like OpenCode that spread state across XDG directories. */
2389
2583
  checkpointDirs?: string[];
@@ -2628,22 +2822,6 @@ declare function saveLocalDir(localPath: string, files: FileMap): void;
2628
2822
  * Gateway mode: no URL → use dashboard API endpoints
2629
2823
  */
2630
2824
  declare function resolveStorageConfig(config: StorageConfig | undefined, isGateway: boolean, gatewayUrl?: string, gatewayApiKey?: string): ResolvedStorageConfig;
2631
- /**
2632
- * List checkpoints (standalone — no Evolve instance needed).
2633
- *
2634
- * BYOK mode: reads directly from S3.
2635
- * Gateway mode: reads EVOLVE_API_KEY from env, calls dashboard API.
2636
- *
2637
- * @example
2638
- * // BYOK
2639
- * const all = await listCheckpoints({ url: "s3://my-bucket/project/" });
2640
- *
2641
- * // Gateway
2642
- * const all = await listCheckpoints({});
2643
- */
2644
- declare function listCheckpoints(config: StorageConfig, options?: {
2645
- limit?: number;
2646
- tag?: string;
2647
- }): Promise<CheckpointInfo[]>;
2825
+ declare function storage(config?: StorageConfig): StorageClient;
2648
2826
 
2649
- export { AGENT_REGISTRY, AGENT_TYPES, Agent, type AgentConfig, type AgentOptions, type AgentOverride, type AgentParser, type AgentRegistryEntry, type AgentResponse, type AgentRuntimeState, type AgentType, type BaseMeta, type BestOfConfig, type BestOfParams, type BestOfResult, type CandidateCompleteEvent, type CheckpointInfo, type ComposioAuthResult, type ComposioConfig, type ComposioConnectionStatus, type ComposioSetup, type EmitOption, type EventHandler, type EventName, Evolve, type EvolveConfig, type EvolveEvents, type ExecuteCommandOptions, type FileMap, type FilterConfig, type FilterParams, type IndexedMeta, type ItemInput, type ItemRetryEvent, JUDGE_PROMPT, type JsonSchema, type JudgeCompleteEvent, type JudgeDecision, type JudgeMeta, type LifecycleEvent, type LifecycleReason, type MapConfig, type MapParams, type McpConfigInfo, type McpServerConfig, type ModelInfo, type OnCandidateCompleteCallback, type OnItemRetryCallback, type OnJudgeCompleteCallback, type OnVerifierCompleteCallback, type OnWorkerCompleteCallback, type OperationType, type OutputEvent, type OutputResult, Pipeline, type PipelineContext, type PipelineEventMap, type PipelineEvents, type PipelineResult, type ProcessInfo, type Prompt, type PromptFn, RETRY_FEEDBACK_PROMPT, type ReasoningEffort, type ReduceConfig, type ReduceMeta, type ReduceParams, type ReduceResult, type ResolvedStorageConfig, type RetryConfig, type RunOptions, SCHEMA_PROMPT, SWARM_RESULT_BRAND, SYSTEM_PROMPT, type SandboxCommandHandle, type SandboxCommandResult, type SandboxCommands, type SandboxCreateOptions, type SandboxFiles, type SandboxInstance, type SandboxLifecycleState, type SandboxProvider, type SandboxRunOptions, type SandboxSpawnOptions, type SchemaValidationOptions, Semaphore, type SessionStatus, type SkillName, type SkillsConfig, type StepCompleteEvent, type StepErrorEvent, type StepEvent, type StepResult, type StepStartEvent, type StorageConfig, type StreamCallbacks, Swarm, type SwarmConfig, type SwarmResult, SwarmResultList, TerminalPipeline, type ToolsFilter, VALIDATION_PRESETS, VERIFY_PROMPT, type ValidationMode, type VerifierCompleteEvent, type VerifyConfig, type VerifyDecision, type VerifyInfo, type VerifyMeta, WORKSPACE_PROMPT, WORKSPACE_SWE_PROMPT, type WorkerCompleteEvent, type WorkspaceMode, applyTemplate, buildWorkerSystemPrompt, createAgentParser, createClaudeParser, createCodexParser, createGeminiParser, executeWithRetry, expandPath, getAgentConfig, getMcpSettingsDir, getMcpSettingsPath, isValidAgentType, isZodSchema, jsonSchemaToString, listCheckpoints, parseNdjsonLine, parseNdjsonOutput, parseQwenOutput, readLocalDir, resolveStorageConfig, saveLocalDir, writeClaudeMcpConfig, writeCodexMcpConfig, writeGeminiMcpConfig, writeMcpConfig, writeQwenMcpConfig, zodSchemaToJson };
2827
+ export { AGENT_REGISTRY, AGENT_TYPES, Agent, type AgentConfig, type AgentOptions, type AgentOverride, type AgentParser, type AgentRegistryEntry, type AgentResponse, type AgentRuntimeState, type AgentType, type BaseMeta, type BestOfConfig, type BestOfParams, type BestOfResult, type CandidateCompleteEvent, type CheckpointInfo, type ComposioAuthResult, type ComposioConfig, type ComposioConnectionStatus, type ComposioSetup, type DownloadCheckpointOptions, type DownloadFilesOptions, type EmitOption, type EventHandler, type EventName, Evolve, type EvolveConfig, type EvolveEvents, type ExecuteCommandOptions, type FileMap, type FilterConfig, type FilterParams, type IndexedMeta, type ItemInput, type ItemRetryEvent, JUDGE_PROMPT, type JsonSchema, type JudgeCompleteEvent, type JudgeDecision, type JudgeMeta, type LifecycleEvent, type LifecycleReason, type MapConfig, type MapParams, type McpConfigInfo, type McpServerConfig, type ModelInfo, type OnCandidateCompleteCallback, type OnItemRetryCallback, type OnJudgeCompleteCallback, type OnVerifierCompleteCallback, type OnWorkerCompleteCallback, type OperationType, type OutputEvent, type OutputResult, Pipeline, type PipelineContext, type PipelineEventMap, type PipelineEvents, type PipelineResult, type ProcessInfo, type Prompt, type PromptFn, RETRY_FEEDBACK_PROMPT, type ReasoningEffort, type ReduceConfig, type ReduceMeta, type ReduceParams, type ReduceResult, type ResolvedStorageConfig, type RetryConfig, type RunCost, type RunOptions, SCHEMA_PROMPT, SWARM_RESULT_BRAND, SYSTEM_PROMPT, type SandboxCommandHandle, type SandboxCommandResult, type SandboxCommands, type SandboxCreateOptions, type SandboxFiles, type SandboxInstance, type SandboxLifecycleState, type SandboxProvider, type SandboxRunOptions, type SandboxSpawnOptions, type SchemaValidationOptions, Semaphore, type SessionCost, type SessionStatus, type SkillName, type SkillsConfig, type StepCompleteEvent, type StepErrorEvent, type StepEvent, type StepResult, type StepStartEvent, type StorageClient, type StorageConfig, type StreamCallbacks, Swarm, type SwarmConfig, type SwarmResult, SwarmResultList, TerminalPipeline, type ToolsFilter, VALIDATION_PRESETS, VERIFY_PROMPT, type ValidationMode, type VerifierCompleteEvent, type VerifyConfig, type VerifyDecision, type VerifyInfo, type VerifyMeta, WORKSPACE_PROMPT, WORKSPACE_SWE_PROMPT, type WorkerCompleteEvent, type WorkspaceMode, applyTemplate, buildWorkerSystemPrompt, createAgentParser, createClaudeParser, createCodexParser, createGeminiParser, executeWithRetry, expandPath, getAgentConfig, getMcpSettingsDir, getMcpSettingsPath, isValidAgentType, isZodSchema, jsonSchemaToString, parseNdjsonLine, parseNdjsonOutput, parseQwenOutput, readLocalDir, resolveStorageConfig, saveLocalDir, storage, writeClaudeMcpConfig, writeCodexMcpConfig, writeGeminiMcpConfig, writeMcpConfig, writeQwenMcpConfig, zodSchemaToJson };