@@ -73,12 +73,28 @@ impl RigAgentBuilder {
7373 get_tier_system_prompt ( self . analysis_type , self . tier )
7474 }
7575
76+ /// Get max output tokens, respecting MCP_CODE_AGENT_MAX_OUTPUT_TOKENS env var
77+ /// This ensures the LLM produces answers within Claude Code's limits
78+ fn get_max_output_tokens ( & self ) -> u64 {
79+ // Check for environment variable override first
80+ if let Ok ( val) = std:: env:: var ( "MCP_CODE_AGENT_MAX_OUTPUT_TOKENS" ) {
81+ if let Ok ( tokens) = val. parse :: < u64 > ( ) {
82+ tracing:: info!( "Rig agent using MCP_CODE_AGENT_MAX_OUTPUT_TOKENS={}" , tokens) ;
83+ return tokens;
84+ }
85+ }
86+
87+ // Fall back to tier-based defaults
88+ self . tier . max_output_tokens ( )
89+ }
90+
7691 /// Build an OpenAI-based agent
7792 #[ cfg( feature = "openai" ) ]
7893 pub fn build_openai ( self ) -> Result < OpenAIAgent > {
7994 let client = RigLLMAdapter :: openai_client ( ) ;
8095 let model = get_model_name ( ) ;
8196 let system_prompt = self . system_prompt ( ) ;
97+ let max_output_tokens = self . get_max_output_tokens ( ) ;
8298 let factory = GraphToolFactory :: new ( self . executor ) ;
8399
84100 info ! (
@@ -93,7 +109,7 @@ impl RigAgentBuilder {
93109 let agent = client
94110 . agent ( & model)
95111 . preamble ( & system_prompt)
96- . max_tokens ( self . tier . max_output_tokens ( ) )
112+ . max_tokens ( max_output_tokens)
97113 . tool ( factory. transitive_dependencies ( ) )
98114 . tool ( factory. circular_dependencies ( ) )
99115 . tool ( factory. call_chain ( ) )
@@ -117,6 +133,7 @@ impl RigAgentBuilder {
117133 let client = RigLLMAdapter :: anthropic_client ( ) ;
118134 let model = get_model_name ( ) ;
119135 let system_prompt = self . system_prompt ( ) ;
136+ let max_output_tokens = self . get_max_output_tokens ( ) ;
120137 let factory = GraphToolFactory :: new ( self . executor ) ;
121138
122139 info ! (
@@ -131,7 +148,7 @@ impl RigAgentBuilder {
131148 let agent = client
132149 . agent ( & model)
133150 . preamble ( & system_prompt)
134- . max_tokens ( self . tier . max_output_tokens ( ) )
151+ . max_tokens ( max_output_tokens)
135152 . tool ( factory. transitive_dependencies ( ) )
136153 . tool ( factory. circular_dependencies ( ) )
137154 . tool ( factory. call_chain ( ) )
@@ -219,6 +236,7 @@ impl RigAgentBuilder {
219236 let client = RigLLMAdapter :: xai_client ( ) ;
220237 let model = get_model_name ( ) ;
221238 let system_prompt = self . system_prompt ( ) ;
239+ let max_output_tokens = self . get_max_output_tokens ( ) ;
222240 let factory = GraphToolFactory :: new ( self . executor ) ;
223241
224242 info ! (
@@ -233,7 +251,7 @@ impl RigAgentBuilder {
233251 let agent = client
234252 . agent ( & model)
235253 . preamble ( & system_prompt)
236- . max_tokens ( self . tier . max_output_tokens ( ) )
254+ . max_tokens ( max_output_tokens)
237255 . tool ( factory. transitive_dependencies ( ) )
238256 . tool ( factory. circular_dependencies ( ) )
239257 . tool ( factory. call_chain ( ) )
@@ -257,6 +275,7 @@ impl RigAgentBuilder {
257275 let client = RigLLMAdapter :: lmstudio_client ( ) ;
258276 let model = get_model_name ( ) ;
259277 let system_prompt = self . system_prompt ( ) ;
278+ let max_output_tokens = self . get_max_output_tokens ( ) ;
260279 let factory = GraphToolFactory :: new ( self . executor ) ;
261280
262281 info ! (
@@ -271,7 +290,7 @@ impl RigAgentBuilder {
271290 let agent = client
272291 . agent ( & model)
273292 . preamble ( & system_prompt)
274- . max_tokens ( self . tier . max_output_tokens ( ) )
293+ . max_tokens ( max_output_tokens)
275294 . tool ( factory. transitive_dependencies ( ) )
276295 . tool ( factory. circular_dependencies ( ) )
277296 . tool ( factory. call_chain ( ) )
@@ -295,6 +314,7 @@ impl RigAgentBuilder {
295314 let client = RigLLMAdapter :: openai_compatible_client ( base_url) ;
296315 let model = get_model_name ( ) ;
297316 let system_prompt = self . system_prompt ( ) ;
317+ let max_output_tokens = self . get_max_output_tokens ( ) ;
298318 let factory = GraphToolFactory :: new ( self . executor ) ;
299319
300320 info ! (
@@ -310,7 +330,7 @@ impl RigAgentBuilder {
310330 let agent = client
311331 . agent ( & model)
312332 . preamble ( & system_prompt)
313- . max_tokens ( self . tier . max_output_tokens ( ) )
333+ . max_tokens ( max_output_tokens)
314334 . tool ( factory. transitive_dependencies ( ) )
315335 . tool ( factory. circular_dependencies ( ) )
316336 . tool ( factory. call_chain ( ) )
0 commit comments