charmbracelet/crush · error

failed to initialize agent: %w

Error message

failed to initialize agent: %w

What it means

Wraps any error returned when the workspace fails to initialize its coder agent in non-interactive mode (InitCoderAgentNonInteractive). The underlying error is preserved via %w so the true cause (model resolution, provider auth, tool init) is chained.

Source

Thrown at internal/cmd/run.go:113

			event.SetContinueLastSession(true)
		}

		if useClientServer() {
			c, ws, cleanup, err := connectToServer(cmd)
			if err != nil {
				return err
			}
			defer cleanup()

			event.AppInitialized()

			if !ws.Config.IsConfigured() {
				return fmt.Errorf("no providers configured - please run 'crush' to set up a provider interactively")
			}

			clientWs := workspace.NewClientWorkspace(c, *ws)
			if err := clientWs.InitCoderAgentNonInteractive(ctx); err != nil {
				return fmt.Errorf("failed to initialize agent: %w", err)
			}

			if sessionID != "" {
				sess, err := resolveSessionByID(ctx, c, ws.ID, sessionID)
				if err != nil {
					return err
				}
				sessionID = sess.ID
			}

			if verbose {
				slog.SetDefault(slog.New(log.New(os.Stderr)))
			}

			return runNonInteractive(ctx, c, ws, prompt, largeModel, smallModel, quiet || verbose, sessionID, useLast)
		}

		ws, cleanup, err := setupLocalWorkspace(cmd)

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Run `crush` interactively once to complete provider setup and credentials
  2. Check that provider API keys are set in the environment or config (crush login)
  3. Validate crushrc/crush.json provider and model entries against the supported model catalog
  4. Re-run with verbose logging to see the wrapped cause

Example fix

// before: unclear root cause
if err := clientWs.InitCoderAgentNonInteractive(ctx); err != nil {
	return fmt.Errorf("failed to initialize agent: %w", err)
}
// after: pre-check config before init
if !ws.Config.IsConfigured() {
	return fmt.Errorf("no providers configured - please run 'crush' to set up a provider interactively")
}
if err := clientWs.InitCoderAgentNonInteractive(ctx); err != nil {
	return fmt.Errorf("failed to initialize agent: %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

cfg, err := config.Load("")
if err != nil {
	return err
}
if !cfg.IsConfigured() {
	return fmt.Errorf("run `crush` interactively first to configure a provider")
}

Try / catch

if err := clientWs.InitCoderAgentNonInteractive(ctx); err != nil {
	var cfgErr *config.InvalidConfigError
	if errors.As(err, &cfgErr) {
		// surface actionable config guidance
	}
	return fmt.Errorf("failed to initialize agent: %w", err)
}

Prevention

When it happens

Trigger: Running `crush run ...` when clientWs.InitCoderAgentNonInteractive(ctx) returns an error — typically because no provider/model could be resolved, provider credentials are invalid, or agent construction fails.

Common situations: Config file exists but providers are half-configured (e.g. env var like ANTHROPIC_API_KEY unset), a model id referenced in crush.json/crushrc no longer exists in the catalog, or MCP servers listed in config fail during agent init.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/0f6af1beba463101. Report an issue: GitHub.