export async function getMaxTokens()

in src/lib/server/providers/index.ts [109:180]


export async function getMaxTokens(
	provider: string,
	modelId: string,
	apiKey: string | undefined
): Promise<number | null> {
	const cache = await readCache();
	const cachedValue = cache[provider]?.[modelId];

	if (cachedValue !== undefined) {
		return cachedValue;
	}

	serverLog(`Cache miss for ${provider} - ${modelId}. Attempting live fetch...`);

	let liveData: number | null = null;
	let fetchedProviderData: MaxTokensCache[string] | null = null;

	try {
		// Pass the received apiKey to the fetcher functions
		switch (provider) {
			case "cohere":
				fetchedProviderData = await fetchCohereData(apiKey); // Pass apiKey
				liveData = fetchedProviderData?.[modelId] ?? null;
				break;
			case "together":
				fetchedProviderData = await fetchTogetherData(apiKey); // Pass apiKey
				liveData = fetchedProviderData?.[modelId] ?? null;
				break;
			case "fireworks-ai":
				fetchedProviderData = await fetchFireworksData(apiKey); // Pass apiKey
				liveData = fetchedProviderData?.[modelId] ?? null;
				break;
			case "hyperbolic":
				fetchedProviderData = await fetchHyperbolicData(apiKey); // Pass apiKey
				liveData = fetchedProviderData?.[modelId] ?? null;
				break;
			case "replicate":
				fetchedProviderData = await fetchReplicateData(apiKey);
				liveData = fetchedProviderData?.[modelId] ?? null;
				break;
			case "nebius":
				fetchedProviderData = await fetchNebiusData(apiKey);
				liveData = fetchedProviderData?.[modelId] ?? null;
				break;
			case "novita":
				fetchedProviderData = await fetchNovitaData(apiKey);
				liveData = fetchedProviderData?.[modelId] ?? null;
				break;
			case "sambanova":
				fetchedProviderData = await fetchSambanovaData(apiKey);
				liveData = fetchedProviderData?.[modelId] ?? null;
				break;
			default:
				serverLog(`Live fetch not supported or implemented for provider: ${provider}`);
				return null;
		}

		if (liveData !== null) {
			serverLog(`Live fetch successful for ${provider} - ${modelId}: ${liveData}`);
			updateCache(provider, modelId, liveData).catch(err => {
				serverError(`Async cache update failed for ${provider} - ${modelId}:`, err);
			});
			return liveData;
		} else {
			serverLog(`Live fetch for ${provider} did not return data for model ${modelId}.`);
			return null;
		}
	} catch (error) {
		serverError(`Error during live fetch for ${provider} - ${modelId}:`, error);
		return null;
	}
}