sigoden/aichat · error
Already in a agent, please run '.exit agent' first to exit…
Error message
Already in a agent, please run '.exit agent' first to exit the current agent.
What it means
Thrown by the agent-entry function in src/config/mod.rs:1498 when an agent session is already active (`config.read().agent.is_some()`). Only one agent may be active per config at a time, so starting another requires explicitly leaving the current one first; the message tells the user the exact command to do so.
Solutions
- Run `.exit agent` (or call `config.exit_agent()`) then start the new agent
- Check `config.read().agent.is_some()` before entry and exit programmatically first
- Refactor scripts to create a fresh session per agent instead of reusing one
Example fix
// before
config.use_agent("other-agent", None, signal).await?;
// after
if config.read().agent.is_some() {
config.exit_agent()?;
}
config.use_agent("other-agent", None, signal).await?; Defensive patterns
Strategy: validation
Validate before calling
if config.read().agent.is_some() {
bail!("Already in an agent; run '.exit agent' before starting another");
} Try / catch
if let Err(e) = config.use_agent(name, session, signal).await {
if e.to_string().contains("Already in a agent") {
config.exit_agent()?;
config.use_agent(name, session, signal).await?;
} else {
return Err(e.into());
}
} Prevention
- Always exit the current agent before entering a new one
- Track agent-session state in your tooling and mirror exit calls
- Use one session per agent in scripts rather than reusing a session
When it happens
Trigger: Running `.agent <other-name>` (or calling the entry function again) while already inside an agent session; scripts that invoke the agent command twice without exiting; nested agent switching without `.exit agent` in between.
Common situations: Users switching between agents interactively and forgetting the exit step; automation that loops over agent names reusing one session; confusion after a failed agent init left state partially set.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- No agent
- Cannot perform this operation because the session has…
- No models
- Unknown client
- Already in a session, please run '.exit session' first to…
AI-assisted analysis of sigoden/aichat@82976d349a (2026-09-09).
Data as JSON: /api/errors/1068b32cbb44f43d.
Report an issue: GitHub.
Appendix: source
Thrown at src/config/mod.rs:1498
}
self.rag_template
.as_deref()
.unwrap_or(RAG_TEMPLATE)
.replace("__CONTEXT__", embeddings)
.replace("__INPUT__", text)
}
pub async fn use_agent(
config: &GlobalConfig,
agent_name: &str,
session_name: Option<&str>,
abort_signal: AbortSignal,
) -> Result<()> {
if !config.read().function_calling {
bail!("Please enable function calling before using the agent.");
}
if config.read().agent.is_some() {
bail!("Already in a agent, please run '.exit agent' first to exit the current agent.");
}
let agent = Agent::init(config, agent_name, abort_signal).await?;
let session = session_name.map(|v| v.to_string()).or_else(|| {
if config.read().macro_flag {
None
} else {
agent.agent_prelude().map(|v| v.to_string())
}
});
config.write().rag = agent.rag();
config.write().agent = Some(agent);
if let Some(session) = session {
config.write().use_session(Some(&session))?;
} else {
config.write().init_agent_shared_variables()?;
}
Ok(())
}View on GitHub (pinned to 82976d349a)