Skip to content

Commit 6b6a2a1

Browse files
committed
Feat: Make max output tokens configurable for agentic workflows
Added MCP_CODE_AGENT_MAX_OUTPUT_TOKENS environment variable and config setting to allow users to customize max output tokens for agentic workflows, overriding tier-based defaults. ## Configuration (config_manager.rs): Added to LLMConfig: - mcp_code_agent_max_output_tokens: Option<usize> field - Environment variable override: MCP_CODE_AGENT_MAX_OUTPUT_TOKENS - Defaults to None (uses tier-based defaults) ## Orchestrator (agentic_orchestrator.rs): AgenticConfig changes: - from_tier_with_override(tier, max_tokens_override) method - from_tier() now delegates to from_tier_with_override(tier, None) AgenticOrchestrator changes: - new_with_override() constructor accepting optional max_tokens - new() delegates to new_with_override(..., None) ## Server Integration (official_server.rs): execute_agentic_workflow() changes: - Reads config.llm.mcp_code_agent_max_output_tokens - Passes override to AgenticOrchestrator::new_with_override() - Falls back to tier defaults if not configured ## CLI Display (bin/codegraph.rs): codegraph config agent-status changes: - Calculates actual max_output_tokens used - Shows "(custom)" or "(tier default)" indicator - JSON output includes: * max_output_tokens: actual value * max_output_tokens_source: "custom" | "tier_default" ## Usage: Environment variable: export MCP_CODE_AGENT_MAX_OUTPUT_TOKENS=8000 Config file (.codegraph.toml): [llm] mcp_code_agent_max_output_tokens = 8000 Tier defaults (if not configured): - Small: 2,048 tokens - Medium: 4,096 tokens - Large: 8,192 tokens - Massive: 16,384 tokens ## Benefits: - Users can test different token limits to optimize coding flow - Override tier defaults without changing LLM configuration - Visible in agent-status command for debugging - Backward compatible (defaults to tier-based values) ~75 lines added
1 parent 5b521d6 commit 6b6a2a1

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

crates/codegraph-core/src/config_manager.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ pub struct LLMConfig {
196196
#[serde(default)]
197197
pub max_completion_token: Option<usize>,
198198

199+
/// MCP code agent maximum output tokens (for agentic workflows)
200+
/// Overrides tier-based defaults if set
201+
#[serde(default)]
202+
pub mcp_code_agent_max_output_tokens: Option<usize>,
203+
199204
/// Reasoning effort for reasoning models: "minimal", "medium", "high"
200205
#[serde(default)]
201206
pub reasoning_effort: Option<String>,
@@ -223,6 +228,7 @@ impl Default for LLMConfig {
223228
insights_mode: default_insights_mode(),
224229
max_tokens: default_max_tokens(),
225230
max_completion_token: None, // Will use max_tokens if not set
231+
mcp_code_agent_max_output_tokens: None, // Use tier-based defaults if not set
226232
reasoning_effort: None, // Only for reasoning models
227233
timeout_secs: default_timeout_secs(),
228234
}
@@ -531,6 +537,12 @@ impl ConfigManager {
531537
config.llm.reasoning_effort = Some(effort);
532538
}
533539

540+
if let Ok(max_output) = std::env::var("MCP_CODE_AGENT_MAX_OUTPUT_TOKENS") {
541+
if let Ok(tokens) = max_output.parse() {
542+
config.llm.mcp_code_agent_max_output_tokens = Some(tokens);
543+
}
544+
}
545+
534546
// Logging
535547
if let Ok(level) = std::env::var("RUST_LOG") {
536548
config.logging.level = level;

crates/codegraph-mcp/src/agentic_orchestrator.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,20 @@ pub struct AgenticConfig {
3333
impl AgenticConfig {
3434
/// Create tier-aware configuration
3535
pub fn from_tier(tier: ContextTier) -> Self {
36-
let (max_steps, max_tokens) = match tier {
36+
Self::from_tier_with_override(tier, None)
37+
}
38+
39+
/// Create tier-aware configuration with optional max_tokens override
40+
pub fn from_tier_with_override(tier: ContextTier, max_tokens_override: Option<usize>) -> Self {
41+
let (max_steps, default_max_tokens) = match tier {
3742
ContextTier::Small => (5, 2048), // Conservative for small models
3843
ContextTier::Medium => (10, 4096), // Moderate for medium models
3944
ContextTier::Large => (15, 8192), // Generous for large models
4045
ContextTier::Massive => (20, 16384), // Very generous for massive models
4146
};
4247

48+
let max_tokens = max_tokens_override.unwrap_or(default_max_tokens);
49+
4350
Self {
4451
max_steps,
4552
max_duration_secs: 300, // 5 minutes max
@@ -204,7 +211,17 @@ impl AgenticOrchestrator {
204211
tool_executor: Arc<GraphToolExecutor>,
205212
tier: ContextTier,
206213
) -> Self {
207-
let config = AgenticConfig::from_tier(tier);
214+
Self::new_with_override(llm_provider, tool_executor, tier, None)
215+
}
216+
217+
/// Create a new agentic orchestrator with optional max_tokens override
218+
pub fn new_with_override(
219+
llm_provider: Arc<dyn LLMProvider>,
220+
tool_executor: Arc<GraphToolExecutor>,
221+
tier: ContextTier,
222+
max_tokens_override: Option<usize>,
223+
) -> Self {
224+
let config = AgenticConfig::from_tier_with_override(tier, max_tokens_override);
208225
Self {
209226
llm_provider,
210227
tool_executor,

crates/codegraph-mcp/src/bin/codegraph.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,13 +1704,19 @@ async fn handle_agent_status(json: bool) -> Result<()> {
17041704
};
17051705

17061706
// Get tier-specific parameters
1707-
let (max_steps, base_limit) = match tier {
1708-
ContextTier::Small => (5, 10),
1709-
ContextTier::Medium => (10, 25),
1710-
ContextTier::Large => (15, 50),
1711-
ContextTier::Massive => (20, 100),
1707+
let (max_steps, base_limit, default_max_tokens) = match tier {
1708+
ContextTier::Small => (5, 10, 2048),
1709+
ContextTier::Medium => (10, 25, 4096),
1710+
ContextTier::Large => (15, 50, 8192),
1711+
ContextTier::Massive => (20, 100, 16384),
17121712
};
17131713

1714+
// Get max output tokens (config override or tier default)
1715+
let max_output_tokens = config
1716+
.llm
1717+
.mcp_code_agent_max_output_tokens
1718+
.unwrap_or(default_max_tokens);
1719+
17141720
// Active MCP tools
17151721
let mcp_tools = vec![
17161722
("enhanced_search", "Search code with AI insights (2-5s)"),
@@ -1766,6 +1772,12 @@ async fn handle_agent_status(json: bool) -> Result<()> {
17661772
"base_search_limit": base_limit,
17671773
"cache_enabled": true,
17681774
"cache_size": 100,
1775+
"max_output_tokens": max_output_tokens,
1776+
"max_output_tokens_source": if config.llm.mcp_code_agent_max_output_tokens.is_some() {
1777+
"custom"
1778+
} else {
1779+
"tier_default"
1780+
},
17691781
},
17701782
"mcp_tools": mcp_tools.iter().map(|(name, desc)| {
17711783
serde_json::json!({
@@ -1851,7 +1863,12 @@ async fn handle_agent_status(json: bool) -> Result<()> {
18511863
"Enabled".green(),
18521864
"100".cyan()
18531865
);
1854-
println!(" Max Output Tokens: {}", "44,200".cyan());
1866+
let max_tokens_display = if config.llm.mcp_code_agent_max_output_tokens.is_some() {
1867+
format!("{} (custom)", max_output_tokens.to_string().cyan())
1868+
} else {
1869+
format!("{} (tier default)", max_output_tokens.to_string().cyan())
1870+
};
1871+
println!(" Max Output Tokens: {}", max_tokens_display);
18551872
println!();
18561873

18571874
// MCP Tools

crates/codegraph-mcp/src/official_server.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,8 +1359,16 @@ impl CodeGraphMCPServer {
13591359
// Create GraphToolExecutor
13601360
let tool_executor = Arc::new(crate::GraphToolExecutor::new(graph_functions));
13611361

1362-
// Create AgenticOrchestrator
1363-
let orchestrator = AgenticOrchestrator::new(llm_provider, tool_executor, tier);
1362+
// Get max_tokens override from config if set
1363+
let max_tokens_override = config.llm.mcp_code_agent_max_output_tokens;
1364+
1365+
// Create AgenticOrchestrator with config override
1366+
let orchestrator = AgenticOrchestrator::new_with_override(
1367+
llm_provider,
1368+
tool_executor,
1369+
tier,
1370+
max_tokens_override,
1371+
);
13641372

13651373
// Get tier-appropriate prompt from PromptSelector
13661374
let prompt_selector = PromptSelector::new();

0 commit comments

Comments
 (0)