{"record":{"id":"0f6af1beba463101","repo":"charmbracelet/crush","slug":"failed-to-initialize-agent-w","errorCode":null,"errorMessage":"failed to initialize agent: %w","messagePattern":"failed to initialize agent: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/run.go","lineNumber":113,"sourceCode":"\t\t\tevent.SetContinueLastSession(true)\n\t\t}\n\n\t\tif useClientServer() {\n\t\t\tc, ws, cleanup, err := connectToServer(cmd)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tdefer cleanup()\n\n\t\t\tevent.AppInitialized()\n\n\t\t\tif !ws.Config.IsConfigured() {\n\t\t\t\treturn fmt.Errorf(\"no providers configured - please run 'crush' to set up a provider interactively\")\n\t\t\t}\n\n\t\t\tclientWs := workspace.NewClientWorkspace(c, *ws)\n\t\t\tif err := clientWs.InitCoderAgentNonInteractive(ctx); err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to initialize agent: %w\", err)\n\t\t\t}\n\n\t\t\tif sessionID != \"\" {\n\t\t\t\tsess, err := resolveSessionByID(ctx, c, ws.ID, sessionID)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\tsessionID = sess.ID\n\t\t\t}\n\n\t\t\tif verbose {\n\t\t\t\tslog.SetDefault(slog.New(log.New(os.Stderr)))\n\t\t\t}\n\n\t\t\treturn runNonInteractive(ctx, c, ws, prompt, largeModel, smallModel, quiet || verbose, sessionID, useLast)\n\t\t}\n\n\t\tws, cleanup, err := setupLocalWorkspace(cmd)","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/cmd/run.go#L95-L131","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run `crush` interactively once to complete provider setup and credentials","Check that provider API keys are set in the environment or config (crush login)","Validate crushrc/crush.json provider and model entries against the supported model catalog","Re-run with verbose logging to see the wrapped cause"],"exampleFix":"// before: unclear root cause\nif err := clientWs.InitCoderAgentNonInteractive(ctx); err != nil {\n\treturn fmt.Errorf(\"failed to initialize agent: %w\", err)\n}\n// after: pre-check config before init\nif !ws.Config.IsConfigured() {\n\treturn fmt.Errorf(\"no providers configured - please run 'crush' to set up a provider interactively\")\n}\nif err := clientWs.InitCoderAgentNonInteractive(ctx); err != nil {\n\treturn fmt.Errorf(\"failed to initialize agent: %w\", err)\n}","handlingStrategy":"validation","validationCode":"cfg, err := config.Load(\"\")\nif err != nil {\n\treturn err\n}\nif !cfg.IsConfigured() {\n\treturn fmt.Errorf(\"run `crush` interactively first to configure a provider\")\n}","typeGuard":null,"tryCatchPattern":"if err := clientWs.InitCoderAgentNonInteractive(ctx); err != nil {\n\tvar cfgErr *config.InvalidConfigError\n\tif errors.As(err, &cfgErr) {\n\t\t// surface actionable config guidance\n\t}\n\treturn fmt.Errorf(\"failed to initialize agent: %w\", err)\n}","preventionTips":["Always run `crush` once interactively in a new environment before `crush run` in CI","Keep provider API keys in env vars referenced by config, not hardcoded","Validate crushrc/crush.json in CI before invoking run"],"tags":["go","agent","initialization","config"],"backgroundTag":"agent-init-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}