diff --git a/agents/ollama.ts b/agents/ollama.ts index c5b2024..c737a61 100644 --- a/agents/ollama.ts +++ b/agents/ollama.ts @@ -117,11 +117,9 @@ async function runOllamaLoop(ctx: AgentRunContext): Promise { model, messages, tools, - keep_alive: "10m", - options: { - think: true, - num_ctx: 262144, - } as Record, + keep_alive: -1, + think: false, + options: { num_ctx: 262144 }, }); } catch (err) { const lastError = err instanceof Error ? err.message : String(err); @@ -162,6 +160,16 @@ async function runOllamaLoop(ctx: AgentRunContext): Promise { } else { log.info("» agent finished (no tool calls)"); } + + // unload the model from memory immediately since we're done with it, to free up resources for future runs. see #829. + try { + await ollama.generate({ model, keep_alive: 0, prompt: "" }); + log.info(`» unloaded model ${model} from Ollama`); + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + log.warning(`» failed to unload model ${model}: ${msg}`); + } + return { success: true, output: assistantMessage.content || undefined, @@ -239,6 +247,16 @@ async function runOllamaLoop(ctx: AgentRunContext): Promise { } log.warning(`» agent hit max iterations (${MAX_ITERATIONS})`); + + // unload the model from memory immediately since we're done with it, to free up resources for future runs. see #829. + try { + await ollama.generate({ model, keep_alive: 0, prompt: "" }); + log.info(`» unloaded model ${model} from Ollama`); + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + log.warning(`» failed to unload model ${model}: ${msg}`); + } + return { success: false, error: `Agent exceeded maximum iterations (${MAX_ITERATIONS})`,