| name | openevidence-cost-tuning |
| description | Optimize OpenEvidence API costs and usage efficiency.
Use when reducing API costs, implementing usage budgets,
or optimizing DeepConsult spend for clinical AI applications.
Trigger with phrases like "openevidence cost", "openevidence billing",
"reduce openevidence spend", "openevidence budget", "openevidence pricing".
|
| allowed-tools | Read, Write, Edit |
| version | 1.0.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
OpenEvidence Cost Tuning
Overview
Optimize OpenEvidence API costs while maintaining clinical decision support quality.
Prerequisites
- OpenEvidence billing dashboard access
- Usage metrics configured
- Understanding of pricing model
- Budget authority
Pricing Model Overview
| Feature | Unit | Typical Cost |
|---|
| Clinical Query | Per query | Included in subscription |
| DeepConsult | Per research synthesis | Premium (100x query cost) |
| API Overage | Per 1000 queries over limit | Tier-dependent |
| Enterprise Features | Monthly | Custom pricing |
Instructions
Step 1: Usage Tracking & Budgeting
interface UsageBudget {
dailyQueryLimit: number;
dailyDeepConsultLimit: number;
monthlyBudget: number;
alertThreshold: number;
}
interface UsageRecord {
queries: number;
deepConsults: number;
estimatedCost: number;
period: 'daily' | 'monthly';
}
export class UsageTracker {
private redis: Redis;
private budget: UsageBudget;
private costPerQuery = 0;
private costPerDeepConsult = 10;
constructor(redis: Redis, budget: UsageBudget) {
this.redis = redis;
this.budget = budget;
}
async trackQuery(userId: string): <> {
today = ().().()[];
month = today.(, );
..(, , );
..(, , );
..(, , );
.(today, month);
}
(: ): <> {
today = ().().()[];
month = today.(, );
..(, , );
..(, , );
..(, , );
.(today, month);
}
(): <{ : ; ?: }> {
today = ().().()[];
dailyQueries = (
..(, ) ||
);
(dailyQueries >= ..) {
{
: ,
: ,
};
}
{ : };
}
(): <{ : ; ?: }> {
today = ().().()[];
dailyDeepConsults = (
..(, ) ||
);
(dailyDeepConsults >= ..) {
{
: ,
: ,
};
}
month = today.(, );
monthlyUsage = .(month);
(monthlyUsage. >= ..) {
{
: ,
: ,
};
}
{ : };
}
(: ): <> {
queries = (
..(, ) ||
);
deepConsults = (
..(, ) ||
);
{
queries,
deepConsults,
: queries * . + deepConsults * .,
: ,
};
}
(: , : ): <> {
monthlyUsage = .(month);
usagePercent = monthlyUsage. / ..;
(usagePercent >= ..) {
.(usagePercent, monthlyUsage);
}
}
(: , : ): <> {
.();
}
}
Step 2: Smart DeepConsult Management
interface DeepConsultDecision {
shouldUseDeepConsult: boolean;
reason: string;
alternativeApproach?: string;
}
export function shouldUseDeepConsult(
question: string,
context: ClinicalContext,
userTier: 'free' | 'professional' | 'enterprise'
): DeepConsultDecision {
const simplePatterns = [
/what is the (dose|dosage)/i,
/half-life of/i,
/contraindications for/i,
/side effects of/i,
];
if (simplePatterns.some(p => p.test(question))) {
return {
shouldUseDeepConsult: false,
reason: 'Simple question - regular query sufficient',
alternativeApproach: 'Use standard clinical query',
};
}
const complexPatterns = [
/compare.*treatments?/i,
/systematic review/i,
/evidence (for|against)/i,
,
,
];
(complexPatterns.( p.(question))) {
(userTier === ) {
{
: ,
: ,
: ,
};
}
{
: ,
: ,
};
}
{
: ,
: ,
: ,
};
}
= * * * ;
(): < | > {
cacheKey = ;
cached = cache.(cacheKey);
(cached) {
.();
cached;
}
;
}
Step 3: User Quotas & Tiering
interface UserQuota {
dailyQueries: number;
monthlyQueries: number;
deepConsultsPerMonth: number;
features: string[];
}
const USER_TIERS: Record<string, UserQuota> = {
free: {
dailyQueries: 10,
monthlyQueries: 100,
deepConsultsPerMonth: 0,
features: ['clinical_query'],
},
professional: {
dailyQueries: 100,
monthlyQueries: 2000,
deepConsultsPerMonth: 10,
features: ['clinical_query', 'deepconsult', 'guidelines'],
},
enterprise: {
dailyQueries: 1000,
monthlyQueries: 30000,
deepConsultsPerMonth: 100,
features: ['clinical_query', 'deepconsult', 'guidelines', 'api_access', 'ehr_integration'],
},
};
export class UserQuotaManager {
(: , : ): <{
: ;
?: ;
?: ;
}> {
user = db..({ : { : userId } });
tier = user?. || ;
quota = [tier];
(!quota..(feature)) {
{
: ,
: ,
};
}
usage = .(userId);
(feature === ) {
remaining = quota. - usage.;
{
: remaining > ,
remaining,
: remaining <= ? : ,
};
}
(feature === ) {
remaining = quota. - usage.;
{
: remaining > ,
remaining,
: remaining <= ? : ,
};
}
{ : };
}
(: ): <> {
{ : , : };
}
}
Step 4: Cost Reporting Dashboard
export async function generateCostReport(
startDate: Date,
endDate: Date
): Promise<CostReport> {
const usage = await aggregateUsage(startDate, endDate);
const report: CostReport = {
period: { start: startDate, end: endDate },
summary: {
totalQueries: usage.queries,
totalDeepConsults: usage.deepConsults,
estimatedCost: calculateCost(usage),
costPerQuery: usage.queries > 0 ? calculateCost(usage) / usage.queries : 0,
},
breakdown: {
bySpecialty: usage.bySpecialty,
byUser: usage.byUser,
byDay: usage.byDay,
},
recommendations: generateRecommendations(usage),
};
return report;
}
function generateRecommendations(usage: any): [] {
: [] = [];
(usage. > usage. * ) {
recommendations.(
);
}
(usage. < ) {
recommendations.(
);
}
topUser = .(usage.)
.( (b[] ) - (a[] ))[];
(topUser && (topUser[] ) > usage. * ) {
recommendations.(
);
}
recommendations;
}
Cost Optimization Checklist
Output
- Usage tracking and budgeting system
- Smart DeepConsult management
- User quota enforcement
- Cost reporting dashboard
- Optimization recommendations
Error Handling
| Cost Issue | Detection | Resolution |
|---|
| Budget exceeded | Alert triggered | Implement throttling or upgrade |
| High DeepConsult costs | Monthly report | Review usage patterns, improve caching |
| Low cache efficiency | Metrics show low hits | Adjust TTL, increase cache size |
| User abuse | Usage concentration | Implement per-user quotas |
Resources
Next Steps
For architecture best practices, see openevidence-reference-architecture.