diff --git a/src/server/features/ai-search/services/promptExplorer.ts b/src/server/features/ai-search/services/promptExplorer.ts index 1328ce8..796262b 100644 --- a/src/server/features/ai-search/services/promptExplorer.ts +++ b/src/server/features/ai-search/services/promptExplorer.ts @@ -28,8 +28,13 @@ import { /** LLM responses are stable enough for a 7-day cache. */ const PROMPT_RESPONSE_TTL_SECONDS = 7 * 24 * 60 * 60; -/** Hard cap on response length to keep payloads sane. */ -const PROMPT_RESPONSE_MAX_TOKENS = 1024; +/** + * Hard cap on response length. Set to the DataForSEO per-call maximum because + * reasoning models (gpt-5, gemini-2.5-pro) count hidden chain-of-thought + * tokens against this budget — at 1024 ChatGPT regularly burns the whole + * budget on reasoning and returns a near-empty visible message. + */ +const PROMPT_RESPONSE_MAX_TOKENS = 4096; type DataforseoClient = ReturnType; @@ -94,7 +99,7 @@ async function runModel( webSearch: args.input.webSearch, webSearchCountryCode: args.input.webSearchCountryCode ?? null, // Bumped when prompt/payload shape changes — busts stale cache entries. - systemPromptV: 4, + systemPromptV: 5, }); const cached = promptExplorerModelResultSchema.safeParse( diff --git a/src/server/lib/dataforseoLlm.ts b/src/server/lib/dataforseoLlm.ts index b9c3d44..39ca7ac 100644 --- a/src/server/lib/dataforseoLlm.ts +++ b/src/server/lib/dataforseoLlm.ts @@ -324,15 +324,19 @@ export async function fetchLlmResponseRaw( input: LlmResponsesInput, ): Promise> { const path = `/v3/ai_optimization/${input.modelSlug}/llm_responses/live`; + // DataForSEO's Gemini endpoint rejects `web_search_country_iso_code` with a + // 40501 "Invalid Field" error. The other three models accept it. + const supportsCountry = input.modelSlug !== "gemini"; const payload = [ { user_prompt: input.userPrompt, model_name: input.modelName, web_search: input.webSearch ?? true, max_output_tokens: clampLimit(input.maxOutputTokens ?? 1024, 256, 4096), - ...(input.webSearchCountryCode && { - web_search_country_iso_code: input.webSearchCountryCode, - }), + ...(supportsCountry && + input.webSearchCountryCode && { + web_search_country_iso_code: input.webSearchCountryCode, + }), }, ];