Skip to content

Optimize AI SDK imports with tree-shaking to reduce startup time #231

@ammar-agent

Description

@ammar-agent

Problem

Current startup time is 6-13s (6s on M3, 13s on M1), primarily due to loading the entire AI SDK and all provider packages. We import the full SDK even though we only use specific providers.

Proposed Solution

Tree-shake AI SDK imports to only load what we actually use:

  1. Audit current usage - Determine which providers we actually need (likely just Anthropic)
  2. Use direct imports - Instead of importing from top-level packages, import specific modules
  3. Lazy-load unused providers - If we support multiple providers, load them on-demand

Example

// ❌ Current: Loads everything
import { anthropic } from '@ai-sdk/anthropic'
import { openai } from '@ai-sdk/openai'  // Loaded even if unused

// ✅ Optimized: Only load what's used
import { anthropic } from '@ai-sdk/anthropic'
// Don't import openai unless needed

Expected Impact

  • Reduce initial module parse time by 30-50%
  • Target startup: 3-6s instead of 6-13s
  • Simpler than worker threads or other complex approaches
  • No UX tradeoffs - just loads less code

Context

This is a better long-term solution than worker threads (PR #226), which moved work to another core but didn't reduce total load time. Real performance gains come from loading less code in the first place.

Implementation Notes

  • May require refactoring service initialization to be provider-specific
  • Could add runtime provider loading if we support multiple backends
  • Should audit all AI SDK imports across codebase

Related: PR #226 (splash screen improvements)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions