Skip to main content

open_agent/types/
agent_options.rs

1/// Configuration for model requests, tool execution, and hooks.
2///
3/// Construct validated options with [`AgentOptions::builder`]. `Default` leaves the
4/// model and base URL empty without validation. Clones share tool handlers.
5/// `max_tokens` and `temperature` remain unset unless explicitly supplied.
6#[derive(Clone)]
7pub struct AgentOptions {
8    /// System prompt that defines the agent's behavior and personality.
9    ///
10    /// This is sent as the first message in the conversation to establish
11    /// context and instructions. Can be empty if no system-level guidance
12    /// is needed.
13    system_prompt: String,
14
15    /// Model identifier for the LLM to use (e.g., "qwen2.5-32b-instruct", "gpt-4").
16    ///
17    /// This must match a model available at the configured `base_url`.
18    /// Different models have varying capabilities for tool use, context
19    /// length, and response quality.
20    model: String,
21
22    /// API endpoint URL (e.g., "http://localhost:1234/v1").
23    ///
24    /// The wire format is chosen by `protocol`: `OpenAiChat` posts to
25    /// `{base_url}/chat/completions`, which local inference servers (LM Studio,
26    /// llama.cpp, vLLM) and most cloud providers support; `Anthropic` posts to
27    /// `{base_url}/messages`.
28    base_url: String,
29
30    /// API authentication key for the provider.
31    ///
32    /// Many local servers don't require authentication, so the default
33    /// "not-needed" is often sufficient. For cloud providers like OpenAI,
34    /// set this to your actual API key.
35    api_key: String,
36
37    /// Caller-supplied HTTP headers applied to every model request.
38    ///
39    /// These may override the protocol defaults, including authentication and content type.
40    /// Header names are matched case-insensitively when the builder replaces an earlier value.
41    headers: BTreeMap<String, String>,
42
43    /// Retained compatibility setting, stored but not enforced by the SDK.
44    ///
45    /// Use `max_tool_iterations` to limit automatic tool rounds. Applications must
46    /// limit conversation turns themselves.
47    max_turns: u32,
48
49    /// Maximum tokens the model should generate in a single response.
50    ///
51    /// `None` uses the provider's default. Lower values constrain response
52    /// length, which can be useful for cost control or ensuring concise answers.
53    /// Note this is separate from the model's context window size.
54    max_tokens: Option<u32>,
55
56    /// Optional sampling temperature. `None` omits the field and lets the server choose.
57    ///
58    /// The builder accepts 0.0 through 2.0; a provider may accept a narrower range or
59    /// reject the parameter entirely.
60    temperature: Option<f32>,
61
62    /// HTTP request timeout in seconds.
63    ///
64    /// A total deadline for each HTTP request, from connecting until its response
65    /// body, the whole stream included, has finished. It applies per API call, not to
66    /// the entire conversation or an automatic tool loop. Increase it for slower
67    /// models or when expecting long responses: a stream still running when it
68    /// elapses ends with a transport error.
69    timeout: u64,
70
71    /// Tools available for the agent to use during conversations.
72    ///
73    /// Tools are wrapped in `Arc` for efficient cloning. When the agent
74    /// receives a tool use request, it looks up the tool by name in this
75    /// vector. Empty by default.
76    tools: Vec<Arc<Tool>>,
77
78    /// Whether the SDK executes tool calls and continues until a text response or
79    /// the configured iteration limit. Defaults to manual execution.
80    auto_execute_tools: bool,
81
82    /// Maximum automatic tool rounds. One round may contain several tool calls.
83    max_tool_iterations: u32,
84
85    /// Whether reasoning is emitted separately from content; defaults to false.
86    ///
87    /// Reasoning never enters assistant text or conversation history.
88    include_reasoning: bool,
89
90    /// The wire protocol this endpoint speaks.
91    ///
92    /// Selects the request path, the auth header, the body shape and the streaming
93    /// vocabulary. Defaults to [`ApiProtocol::OpenAiChat`], which is what every endpoint the
94    /// SDK supported before 0.9.0 speaks, so an existing configuration keeps its behaviour.
95    protocol: ApiProtocol,
96
97    /// Hooks for prompt submission and automatic pre/post-tool execution.
98    hooks: Hooks,
99}
100
101/// Omits credentials and shows tool counts instead of handlers.
102impl std::fmt::Debug for AgentOptions {
103    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104        f.debug_struct("AgentOptions")
105            .field("system_prompt", &self.system_prompt)
106            .field("model", &self.model)
107            .field("base_url", &self.base_url)
108            // Mask API key to prevent credential leakage in logs
109            .field("api_key", &"***")
110            .field("max_turns", &self.max_turns)
111            .field("max_tokens", &self.max_tokens)
112            .field("temperature", &self.temperature)
113            .field("timeout", &self.timeout)
114            // Show tool count instead of trying to debug Arc<Tool> contents
115            .field("tools", &format!("{} tools", self.tools.len()))
116            .field("auto_execute_tools", &self.auto_execute_tools)
117            .field("max_tool_iterations", &self.max_tool_iterations)
118            .field("include_reasoning", &self.include_reasoning)
119            .field("protocol", &self.protocol)
120            .field("hooks", &self.hooks)
121            .finish()
122    }
123}
124
125/// Defaults with an empty model and base URL. Use the builder for runtime validation.
126impl Default for AgentOptions {
127    fn default() -> Self {
128        Self {
129            system_prompt: String::new(),
130            model: String::new(),
131            base_url: String::new(),
132            api_key: "not-needed".to_string(),
133            headers: BTreeMap::new(),
134            max_turns: 1,
135            max_tokens: None,
136            temperature: None,
137            timeout: 60,
138            tools: Vec::new(),
139            auto_execute_tools: false,
140            max_tool_iterations: 5,
141            hooks: Hooks::new(),
142            include_reasoning: false,
143            protocol: ApiProtocol::OpenAiChat,
144        }
145    }
146}
147
148impl AgentOptions {
149    /// Creates a builder that requires a model and base URL.
150    ///
151    /// See [`AgentOptionsBuilder`] for an example.
152    pub fn builder() -> AgentOptionsBuilder {
153        AgentOptionsBuilder::default()
154    }
155
156    /// Returns the system prompt.
157    pub fn system_prompt(&self) -> &str {
158        &self.system_prompt
159    }
160
161    /// Returns the model identifier.
162    pub fn model(&self) -> &str {
163        &self.model
164    }
165
166    /// Returns the base URL.
167    pub fn base_url(&self) -> &str {
168        &self.base_url
169    }
170
171    /// Returns the API key.
172    pub fn api_key(&self) -> &str {
173        &self.api_key
174    }
175
176    /// Returns the caller-supplied HTTP headers.
177    pub fn headers(&self) -> &BTreeMap<String, String> {
178        &self.headers
179    }
180
181    /// Returns the retained `max_turns` compatibility value.
182    ///
183    /// The SDK does not enforce it. Use [`max_tool_iterations`](Self::max_tool_iterations)
184    /// for automatic tool rounds and enforce conversation limits in your application.
185    pub fn max_turns(&self) -> u32 {
186        self.max_turns
187    }
188
189    /// Returns the maximum tokens setting.
190    pub fn max_tokens(&self) -> Option<u32> {
191        self.max_tokens
192    }
193
194    /// Returns the sampling temperature, or `None` when the server should choose.
195    pub fn temperature(&self) -> Option<f32> {
196        self.temperature
197    }
198
199    /// Returns the wire protocol this endpoint speaks.
200    pub fn protocol(&self) -> ApiProtocol {
201        self.protocol
202    }
203
204    /// Returns the HTTP timeout in seconds.
205    pub fn timeout(&self) -> u64 {
206        self.timeout
207    }
208
209    /// Returns a reference to the tools vector.
210    pub fn tools(&self) -> &[Arc<Tool>] {
211        &self.tools
212    }
213
214    /// Returns whether automatic tool execution is enabled.
215    pub fn auto_execute_tools(&self) -> bool {
216        self.auto_execute_tools
217    }
218
219    /// Returns the maximum tool execution iterations.
220    pub fn max_tool_iterations(&self) -> u32 {
221        self.max_tool_iterations
222    }
223
224    /// Returns a reference to the hooks configuration.
225    pub fn hooks(&self) -> &Hooks {
226        &self.hooks
227    }
228
229    /// Returns whether the reasoning channel is surfaced to the caller.
230    pub fn include_reasoning(&self) -> bool {
231        self.include_reasoning
232    }
233}