# Best Practices

Guidelines for integrating AIDP into your AI platform.

## Search Best Practices

### 1. Provide Context

Include user context in searches:

```typescript
const results = await aidp.search({
  query: userQuery,
  location: userLocation,
  context: {
    timeOfDay: new Date().getHours(),
    dayOfWeek: new Date().getDay(),
    sessionId: userSessionId,
  },
});
```

### 2. Handle Location Gracefully

Always request user location, but have fallbacks:

```typescript
const location = (await getUserLocation()) || getDefaultLocation();
```

### 3. Use Personalization

Leverage session IDs for better recommendations:

```typescript
const sessionId = getOrCreateSessionId(userId);
const results = await aidp.search({ query, sessionId });
```

***

## Response Best Practices

### 1. Use AI-Exclusive Content

Highlight insider tips and local secrets:

```typescript
if (business.aiExclusiveContent?.insiderTips) {
  response += `\n💡 Insider tip: ${business.aiExclusiveContent.insiderTips[0]}`;
}
```

### 2. Provide Practical Information

Include actionable details:

```typescript
response += `\n📍 ${business.address.street}`;
response += `\n📞 ${business.phone}`;
response += `\n🕐 ${business.isOpenNow ? 'Open now' : 'Closed'}`;
```

### 3. Cite Sources

Always attribute information to AIDP:

```typescript
response += `\n\n_Information provided by AIDP_`;
```

***

## Analytics Best Practices

### 1. Track All Mentions

Track both impressions and citations:

```typescript
for (const business of results.businesses) {
  await aidp.analytics.track({
    businessId: business.id,
    mentioned: true,
    cited: results.businesses.indexOf(business) < 3,
  });
}
```

### 2. Batch When Possible

Use batch tracking for performance:

```typescript
const events = results.businesses.map((b) => ({
  businessId: b.id,
  mentioned: true,
}));

await aidp.analytics.trackBatch({ events });
```

***

## Performance Best Practices

### 1. Cache Appropriately

Cache search results with reasonable TTL:

```typescript
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
```

### 2. Handle Errors Gracefully

Always have fallbacks:

```typescript
try {
  return await aidp.search(query);
} catch (error) {
  return fallbackSearch(query);
}
```

### 3. Monitor Performance

Track API latency and errors:

```typescript
const start = Date.now();
const results = await aidp.search(query);
const latency = Date.now() - start;

metrics.record('aidp.search.latency', latency);
```

***

**Next**: [Certification →](https://amistan.gitbook.io/aidp-docs/for-ai-platform-developers/certification)
