-
Notifications
You must be signed in to change notification settings - Fork 859
Feat: Cost optimizations via model configs and prompt improvements #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Claude finished @AshishKumar4's task —— View job Review CompleteI've completed the PR description generation and comprehensive code review for this cost optimization PR. What I Did:
Review Verdict: REQUEST CHANGES The PR has excellent intent but contains a critical bug where the environment-based config switching won't work due to incorrect |
worker/agents/inferutils/config.ts
Outdated
| import { env } from 'cloudflare:workers'; | ||
|
|
||
| export const AGENT_CONFIG: AgentConfig = { | ||
| export const AGENT_CONFIG: AgentConfig = env.PLATFORM_MODEL_PROVIDERS ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Runtime Environment Access Issue
This pattern won't work correctly. The env import from cloudflare:workers is not available at module initialization time. In Cloudflare Workers, environment bindings are passed at runtime through the request handler.
Current approach:
import { env } from 'cloudflare:workers';
export const AGENT_CONFIG: AgentConfig = env.PLATFORM_MODEL_PROVIDERS ? { ... } : { ... };Why it fails:
envis undefined during module initialization- Config will always use the default branch
- Production deployments won't get the optimized config
Recommended fix:
Make AGENT_CONFIG a function that accepts the environment:
export function getAgentConfig(env: Env): AgentConfig {
return env.PLATFORM_MODEL_PROVIDERS ? {
// production config
} : {
// default config
};
}Then update all call sites to use getAgentConfig(env) instead of AGENT_CONFIG.
worker/agents/inferutils/config.ts
Outdated
| You may need to provide API keys for these models in your environment or use Cloudflare AI Gateway unified billing | ||
| for seamless model access without managing multiple keys. | ||
| */ | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
High: DRY Principle Violation
The two config branches duplicate ~95 lines of configuration. This violates the DRY principle from CLAUDE.md and creates maintenance burden.
Impact:
- Bug fixes need to be applied in two places
- Risk of configs diverging over time
- Harder to understand which values differ
Recommended refactor:
const DEFAULT_CONFIG: AgentConfig = {
templateSelection: { /* ... */ },
// ... default values
};
const PRODUCTION_OVERRIDES: Partial<AgentConfig> = {
blueprint: {
name: AIModels.GEMINI_3_PRO_PREVIEW,
// ... only production differences
},
// ... other overrides
};
export const AGENT_CONFIG: AgentConfig = env.PLATFORM_MODEL_PROVIDERS
? { ...DEFAULT_CONFIG, ...PRODUCTION_OVERRIDES }
: DEFAULT_CONFIG;This approach:
- Defines defaults once
- Only specifies production differences
- Makes it clear what changes in production
Code Quality & Security ReviewRecommendation: REQUEST CHANGES I've reviewed this PR focused on cost optimization changes. While the intent is excellent, there are critical implementation issues that need to be addressed before merging. Code QualityCritical Issues:
Minor Issues:
Positive Notes:
SecurityNo security vulnerabilities identified
SummaryThis PR contains valuable cost optimizations, but the critical environment access bug means the production optimizations won't actually work in practice. The code would always use the default Gemini-only config regardless of the Required Actions:
Once these issues are addressed, this will be a solid cost optimization that maintains code quality standards. |
|
Claude finished @AshishKumar4's task —— View job Code Review UpdateTodo List:
Code Quality & Security ReviewRecommendation: REQUEST CHANGES This PR contains valuable cost optimization intent, but the implementation has critical issues that prevent it from working as designed. The previous review's concerns remain unaddressed in the latest commits. Code QualityCritical Issues:
Minor Issues:
Positive Aspects:
SecurityNo security vulnerabilities identified
SummaryThe cost optimization strategy is sound, but the implementation is fundamentally broken. The critical issue is that Cloudflare Workers'
Required Actions:
Evidence: I scanned the entire worker codebase and confirmed this is the ONLY file attempting module-level Once these issues are fixed, this will be a solid cost optimization that maintains the project's code quality standards. |
worker/agents/inferutils/config.ts
Outdated
| import { env } from 'cloudflare:workers'; | ||
|
|
||
| export const AGENT_CONFIG: AgentConfig = { | ||
| export const AGENT_CONFIG: AgentConfig = env.PLATFORM_MODEL_PROVIDERS ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CRITICAL: Module-level env access will not work
The env import from cloudflare:workers is not available at module initialization time. This line will fail or return undefined, causing the config to always use the default branch.
Why this fails:
- In Cloudflare Workers,
envbindings are provided at request time, not module load time - This is the only file in the entire worker codebase that attempts to access
envat the module level - All other files (sandboxSdkClient.ts, feedback.ts, etc.) correctly access
envinside functions/methods
Evidence from codebase:
$ grep -r "export const.*env\." worker
worker/agents/inferutils/config.ts:12:export const AGENT_CONFIG: AgentConfig = env.PLATFORM_MODEL_PROVIDERS ?
# This is the ONLY occurrence - all other env access is inside functionsRequired fix:
export function getAgentConfig(env: Env): AgentConfig {
return env.PLATFORM_MODEL_PROVIDERS ? {
// production config
} : {
// default config
};
}Then update all call sites in:
worker/agents/inferutils/infer.ts(line 157:AGENT_CONFIG[agentActionName])- Any other files that import
AGENT_CONFIG
Impact: Without this fix, the production cost optimizations will never activate, defeating the entire purpose of this PR.
worker/agents/inferutils/config.ts
Outdated
| You may need to provide API keys for these models in your environment or use Cloudflare AI Gateway unified billing | ||
| for seamless model access without managing multiple keys. | ||
| */ | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HIGH: DRY Principle Violation - 95 lines duplicated
The two config branches duplicate almost all configuration, violating CLAUDE.md's core requirement: "Never copy-paste code - refactor into shared functions."
Impact:
- Bug fixes need to be applied twice (error-prone)
- Configs will drift over time
- Unclear which values actually differ between prod/default
- Maintenance burden increases
Recommended refactor approach:
// Define base config once
const BASE_CONFIG: AgentConfig = {
templateSelection: {
name: AIModels.GEMINI_2_5_FLASH_LITE,
max_tokens: 2000,
fallbackModel: AIModels.GEMINI_2_5_FLASH,
temperature: 0.6,
},
// ... other common values
};
// Define only production differences
const PRODUCTION_OVERRIDES: DeepPartial<AgentConfig> = {
blueprint: {
name: AIModels.OPENAI_5_MINI,
max_tokens: 32000,
temperature: 1.0,
},
projectSetup: {
name: AIModels.GROK_4_FAST,
},
// ... only values that differ
};
export function getAgentConfig(env: Env): AgentConfig {
return env.PLATFORM_MODEL_PROVIDERS
? deepMerge(BASE_CONFIG, PRODUCTION_OVERRIDES)
: BASE_CONFIG;
}Benefits:
- Single source of truth for defaults
- Clear visibility into production differences
- Eliminates duplication
- Easier to maintain and extend
worker/agents/inferutils/config.ts
Outdated
| export const AGENT_CONFIG: AgentConfig = { | ||
| export const AGENT_CONFIG: AgentConfig = env.PLATFORM_MODEL_PROVIDERS ? | ||
| //====================================================================================== | ||
| // ATTENTION! This config is will most likely NOT work right away! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: Typo in comment
"is will most likely" should be "will most likely"
52fa74b to
3f9ccf7
Compare
…imits Split AGENT_CONFIG into platform-specific and vanilla deployments based on PLATFORM_MODEL_PROVIDERS environment variable. Add documentation comments explaining config usage. Reduce deepDebugger max tool calling depth from 100 to 50 and conversation compactification threshold from 100k to 50k tokens. Add Google Vertex AI models (GPT OSS 120B and Kimi K2 Thinking) to model catalog. Enhance blueprint system prompt to emphas
Switch blueprint agent from Gemini 3 Pro Preview to OpenAI 5 Mini with reduced max tokens (32k). Update firstPhaseImplementation and phaseImplementation agents to use Gemini 2.5 Pro instead of Gemini 3 Pro Preview. Lower firstPhaseImplementation temperature from 1.0 to 0.2 for more consistent output. Update blueprint system prompt to clarify project seriousness by replacing "toy or educational" with "toy or demo".
0fe4674 to
a890440
Compare
|
Claude finished @AshishKumar4's task —— View job Code Quality & Security ReviewRecommendation: REQUEST CHANGES This PR contains valuable cost optimization intent, but the implementation has critical issues that prevent it from working as designed. The same critical issues identified in previous reviews remain unaddressed. Code QualityCritical Issues:
Minor Issues:
Positive Aspects:
SecurityNo security vulnerabilities identified
SummaryThe cost optimization strategy is sound, but the implementation is fundamentally broken. The critical issue is that Cloudflare Workers'
Evidence: I scanned the entire worker codebase and confirmed this is the ONLY file attempting module-level $ grep -r "export const.*= env\." worker/
worker/agents/inferutils/config.ts:12:export const AGENT_CONFIG: AgentConfig = env.PLATFORM_MODEL_PROVIDERS ?All other files (feedback.ts, web-search.ts, sandboxSdkClient.ts, etc.) correctly access Required Actions:
Once these issues are fixed, this will be a solid cost optimization that maintains the project's code quality standards. PR DescriptionThe PR description is comprehensive and well-structured. It clearly explains:
No changes needed to the description. |
worker/agents/inferutils/config.ts
Outdated
| import { env } from 'cloudflare:workers'; | ||
|
|
||
| export const AGENT_CONFIG: AgentConfig = { | ||
| export const AGENT_CONFIG: AgentConfig = env.PLATFORM_MODEL_PROVIDERS ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CRITICAL: Module-level env access will not work
The env import from cloudflare:workers is not available at module initialization time. This line will fail or return undefined, causing the config to always use the default branch.
Why this fails:
- In Cloudflare Workers,
envbindings are provided at request time, not module load time - I verified: this is the only file in the entire worker codebase that attempts to access
envat the module level - All other files (feedback.ts, web-search.ts, sandboxSdkClient.ts, etc.) correctly access
envinside functions/methods
Evidence:
$ grep -r "export const.*= env\." worker/
worker/agents/inferutils/config.ts:12:export const AGENT_CONFIG: AgentConfig = env.PLATFORM_MODEL_PROVIDERS ?
# This is the ONLY occurrenceRequired fix:
export function getAgentConfig(env: Env): AgentConfig {
return env.PLATFORM_MODEL_PROVIDERS ? {
// production config
} : {
// default config
};
}Then update all call sites in:
worker/agents/inferutils/infer.ts(line 157:AGENT_CONFIG[agentActionName])worker/database/services/ModelConfigService.ts(lines 58, 88, 195)worker/agents/core/simpleGeneratorAgent.ts(line 1411)
Impact: Without this fix, the production cost optimizations will never activate, defeating the entire purpose of this PR.
worker/agents/inferutils/config.ts
Outdated
| You may need to provide API keys for these models in your environment or use Cloudflare AI Gateway unified billing | ||
| for seamless model access without managing multiple keys. | ||
| */ | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HIGH: DRY Principle Violation - 95+ lines duplicated
The two config branches duplicate almost all configuration, violating CLAUDE.md's core requirement: "Never copy-paste code - refactor into shared functions."
Impact:
- Bug fixes need to be applied twice (error-prone)
- Configs will drift over time
- Unclear which values actually differ between prod/default
- Maintenance burden increases
What actually differs between production and default configs?
Looking at the diff, only these fields change:
blueprint: Different model, token limits, temperatureprojectSetup: Different model in productionphaseGeneration: Different model in productionconversationalResponse: Different model in productiondeepDebugger: Different model and temperaturefileRegeneration: Different model and temperaturefirstPhaseImplementation: Different token limits and temperature
Recommended refactor approach:
// Define base config once
const BASE_CONFIG: AgentConfig = {
templateSelection: {
name: AIModels.GEMINI_2_5_FLASH_LITE,
max_tokens: 2000,
fallbackModel: AIModels.GEMINI_2_5_FLASH,
temperature: 0.6,
},
// ... all common default values
};
// Define only production differences
const PRODUCTION_OVERRIDES: DeepPartial<AgentConfig> = {
blueprint: {
name: AIModels.OPENAI_5_MINI,
max_tokens: 32000,
temperature: 1.0,
},
projectSetup: {
name: AIModels.GROK_4_FAST,
},
// ... only values that differ
};
export function getAgentConfig(env: Env): AgentConfig {
return env.PLATFORM_MODEL_PROVIDERS
? deepMerge(BASE_CONFIG, PRODUCTION_OVERRIDES)
: BASE_CONFIG;
}Benefits:
- Single source of truth for defaults
- Clear visibility into production differences (only ~20 lines instead of 95)
- Eliminates duplication
- Easier to maintain and extend
worker/agents/inferutils/config.ts
Outdated
| export const AGENT_CONFIG: AgentConfig = { | ||
| export const AGENT_CONFIG: AgentConfig = env.PLATFORM_MODEL_PROVIDERS ? | ||
| //====================================================================================== | ||
| // ATTENTION! This config is will most likely NOT work right away! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: Typo in comment
"is will most likely" should be "will most likely"
|
Claude finished @AshishKumar4's task —— View job Code Quality & Security Review CompleteRecommendation: REQUEST CHANGES This PR has excellent cost optimization intent, but contains a critical implementation bug that prevents the production optimizations from working. The same critical issue identified in three previous reviews remains unaddressed. Code QualityCritical Issues:
Evidence of Critical Issue: I scanned the codebase and confirmed this is the only file attempting module-level # All other env imports use it inside functions
worker/agents/tools/toolkit/feedback.ts: env.SENTRY_DSN inside submitFeedbackImplementation()
worker/services/sandbox/sandboxSdkClient.ts: env.MAX_SANDBOX_INSTANCES inside methods
worker/agents/inferutils/config.ts: env.PLATFORM_MODEL_PROVIDERS at module level ❌Positive Aspects:
SecurityNo security vulnerabilities identified
SummaryThe cost optimization strategy is sound, but the implementation is fundamentally broken. Cloudflare Workers'
Required Actions:
Once these issues are fixed, this will be a solid cost optimization that maintains code quality standards. Review posted: Full review comment | Inline comments on critical issues |
| }, | ||
| }; | ||
|
|
||
| export const AGENT_CONFIG: AgentConfig = env.PLATFORM_MODEL_PROVIDERS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CRITICAL: Module-level env access will not work in Cloudflare Workers
The env from cloudflare:workers is not available at module initialization time. This line will cause the config to always use the default branch, completely defeating the purpose of this PR's cost optimizations.
Why this fails:
- Cloudflare Workers provide
envbindings only at request time, not during module load - I verified: this is the only file in the entire worker codebase attempting module-level
envaccess - All other files (feedback.ts, sandboxSdkClient.ts, etc.) correctly access
envinside functions
Example from feedback.ts (correct pattern):
const submitFeedbackImplementation = async (args: FeedbackArgs) => {
const sentryDsn = env.SENTRY_DSN; // ✅ Inside function - works
// ...
}Required fix:
export function getAgentConfig(env: Env): AgentConfig {
return env.PLATFORM_MODEL_PROVIDERS
? PLATFORM_AGENT_CONFIG
: DEFAULT_AGENT_CONFIG;
}Then update all call sites:
worker/agents/inferutils/infer.ts(line 157)worker/database/services/ModelConfigService.tsworker/agents/core/simpleGeneratorAgent.ts
Impact: Without this fix, production cost optimizations will NEVER activate.
| You may need to provide API keys for these models in your environment or use | ||
| Cloudflare AI Gateway unified billing for seamless model access without managing multiple keys. | ||
| */ | ||
| const PLATFORM_AGENT_CONFIG: AgentConfig = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HIGH PRIORITY: DRY Principle Violation
The two config branches (PLATFORM_AGENT_CONFIG and DEFAULT_AGENT_CONFIG) duplicate most of the configuration. This violates CLAUDE.md's core requirement: "Never copy-paste code - refactor into shared functions."
Current state:
- 53 lines for PLATFORM_AGENT_CONFIG (lines 58-110)
- 46 lines for DEFAULT_AGENT_CONFIG (lines 116-162)
- Most configs are nearly identical
What actually differs?
Only these fields change between production and default:
blueprint: Different model (OPENAI_5_MINI vs GEMINI_2_5_PRO), token limits, temperatureprojectSetup: GROK_4_FAST vs GEMINI_2_5_PROphaseGeneration: GROK_4_FAST vs GEMINI_2_5_PROconversationalResponse: Different model and temperaturedeepDebugger: OPENAI_5_MINI vs GEMINI_2_5_PRO, different temperaturefileRegeneration: Different token limits
Recommended refactor:
const BASE_CONFIG: AgentConfig = {
...COMMON_AGENT_CONFIGS,
blueprint: {
name: AIModels.GEMINI_2_5_PRO,
reasoning_effort: 'medium',
max_tokens: 64000,
fallbackModel: AIModels.GEMINI_2_5_FLASH,
temperature: 0.7,
},
// ... all other defaults
};
const PRODUCTION_OVERRIDES: Partial<AgentConfig> = {
blueprint: {
name: AIModels.OPENAI_5_MINI,
max_tokens: 32000,
temperature: 1.0,
// ... only different values
},
// ... only overrides
};
export function getAgentConfig(env: Env): AgentConfig {
return env.PLATFORM_MODEL_PROVIDERS
? deepMerge(BASE_CONFIG, PRODUCTION_OVERRIDES)
: BASE_CONFIG;
}Benefits:
- Single source of truth for defaults
- Clear visibility into what actually differs (only ~15 lines of overrides vs 99 lines of duplication)
- Easier maintenance and reduced bug risk
Code Quality & Security ReviewRecommendation: REQUEST CHANGES This PR has excellent cost optimization intent, but contains a critical implementation bug that prevents the production optimizations from working. The same critical issue identified in three previous reviews remains unaddressed. Code QualityCritical Issues:
Evidence of Critical Issue: I scanned the codebase and confirmed this is the only file attempting module-level # All other env imports use it inside functions
worker/agents/tools/toolkit/feedback.ts: env.SENTRY_DSN inside submitFeedbackImplementation()
worker/services/sandbox/sandboxSdkClient.ts: env.MAX_SANDBOX_INSTANCES inside methods
worker/agents/inferutils/config.ts: env.PLATFORM_MODEL_PROVIDERS at module level ❌Positive Aspects:
SecurityNo security vulnerabilities identified
SummaryThe cost optimization strategy is sound, but the implementation is fundamentally broken. Cloudflare Workers'
Required Actions:
Once these issues are fixed, this will be a solid cost optimization that maintains code quality standards. |
Summary
This PR introduces cost optimizations across the agent system through refined model configurations, reduced token limits, and improved prompt strategies.
Changes
Model Configuration Architecture (worker/agents/inferutils/config.ts)
env.PLATFORM_MODEL_PROVIDERSflagNew Model Support (worker/agents/inferutils/config.types.ts)
Token Limit Reductions
Prompt Improvements (worker/agents/planning/blueprint.ts)
Motivation
The platform's LLM costs were growing due to:
This PR addresses all four issues, reducing costs while maintaining or improving output quality.
Testing
PLATFORM_MODEL_PROVIDERSflagBreaking Changes
None. The dual-config system maintains backward compatibility. Deployments without
PLATFORM_MODEL_PROVIDERScontinue using Gemini-only defaults.Related Issues
This PR description was automatically generated by Claude Code