Implementation:Ollama Ollama XCmd Run
| Knowledge Sources | |
|---|---|
| Domains | CLI, Agent Loop |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Implements the interactive agent chat loop for ollama run, orchestrating the conversation between the user, the LLM, and tools (bash, web search, web fetch).
Description
The Chat function manages a streaming conversation with the Ollama API, handling tool calls from the LLM by routing them through the tool registry and approval manager. Detects local vs. cloud models to apply different token limits for tool output truncation (4k tokens for local, 10k for cloud). Supports cloud sign-in flow by polling the Whoami endpoint. Renders tool outputs with formatting, manages thinking display, and handles word wrapping for terminal display.
Usage
Called as the main entry point for interactive AI agent sessions with tool use capabilities in the experimental ollama run mode.
Code Reference
Source Location
- Repository: Ollama
- File: x/cmd/run.go
- Lines: 1-1111
Signature
type RunOptions struct {
Model string
Messages []api.Message
WordWrap bool
Format string
System string
Options map[string]any
KeepAlive *api.Duration
Think *api.ThinkValue
HideThinking bool
Verbose bool
Tools *tools.Registry
Approval *agent.ApprovalManager
YoloMode bool
}
func Chat(ctx context.Context, opts RunOptions) (*api.Message, error)
func truncateToolOutput(output, modelName string) string
Import
import "github.com/ollama/ollama/x/cmd"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| ctx | context.Context | Yes | Context for cancellation |
| opts | RunOptions | Yes | Configuration including model name, tools, and approval manager |
Outputs
| Name | Type | Description |
|---|---|---|
| message | *api.Message | Final assistant message with content and tool calls |
| error | error | Non-nil on API or execution errors |
Usage Examples
registry := tools.DefaultRegistry()
approval := agent.NewApprovalManager()
msg, err := cmd.Chat(ctx, cmd.RunOptions{
Model: "qwen3:latest",
Messages: messages,
Tools: registry,
Approval: approval,
WordWrap: true,
})