Skip to content

Commit f0cf9f8

Browse files
committed
update docs
1 parent 857e12f commit f0cf9f8

File tree

1 file changed

+54
-34
lines changed

1 file changed

+54
-34
lines changed

MyApp.Client/_posts/ai-chat.md

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -746,40 +746,6 @@ class MyService(IChatClients clients)
746746
}
747747
```
748748

749-
## Persist AI Chat History
750-
751-
By default AI Chat is designed to be minimally invasive and doesn't require anything other than the API Keys
752-
needed to access the AI Models it should use.
753-
754-
If preferred you can choose to persist AI Chat History made through the external ChatCompletion API with the
755-
`OnChatCompletionSuccessAsync` and `OnChatCompletionFailedAsync` callbacks which can be used to store successful
756-
and failed requests in your preferred data store using the included
757-
[ChatCompletionLog](https://github.com/ServiceStack/ServiceStack/blob/main/ServiceStack/src/ServiceStack.AI.Chat/ChatCompletionLog.cs)
758-
or your own data model:
759-
760-
```csharp
761-
public class ConfigureAiChat : IHostingStartup
762-
{
763-
public void Configure(IWebHostBuilder builder) => builder
764-
.ConfigureServices(services => {
765-
services.AddPlugin(new ChatFeature
766-
{
767-
OnChatCompletionSuccessAsync = async (request, response, req) => {
768-
using var db = await req.Resolve<IDbConnectionFactory>().OpenAsync();
769-
await db.InsertAsync(req.ToChatCompletionLog(request, response));
770-
},
771-
OnChatCompletionFailedAsync = async (request, exception, req) => {
772-
using var db = await req.Resolve<IDbConnectionFactory>().OpenAsync();
773-
await db.InsertAsync(req.ToChatCompletionLog(request, exception));
774-
},
775-
});
776-
}).ConfigureAppHost(appHost => {
777-
using var db = appHost.Resolve<IDbConnectionFactory>().Open();
778-
db.CreateTableIfNotExists<ChatCompletionLog>();
779-
});
780-
}
781-
```
782-
783749
### Compatible with llms.py
784750

785751
The other benefit of simple configuration and simple solutions, is that they're easy to implement. A perfect example
@@ -819,3 +785,57 @@ Incidentally as [llms.py UI](https://servicestack.net/posts/llms-py-ui) and AI C
819785
**import/export** features to transfer your AI Chat History between them.
820786

821787
Checkout the [llms.py GitHub repo](https://github.com/ServiceStack/llms) for even more features.
788+
789+
## AI Chat History Persistence
790+
791+
Enabling chat history persistence allows you to maintain a complete audit trail of all AI interactions, track token consumption, monitor costs across providers and models, and analyze usage patterns over time that captures every
792+
request and response flowing through AI Chat's UI, external OpenAI endpoints and internal `IChatStore` requests.
793+
794+
### Database Storage Options
795+
796+
ServiceStack provides two storage implementations to suit different deployment scenarios:
797+
798+
`DbChatStore` - A universal solution that stores chat history in a single table compatible with any RDBMS
799+
[supported by OrmLite](https://docs.servicestack.net/ormlite/getting-started):
800+
801+
```csharp
802+
services.AddSingleton<IChatStore,DbChatStore>();
803+
```
804+
805+
`PostgresChatStore` - An optimized implementation for PostgreSQL that leverages monthly table partitioning for improved query performance and data management:
806+
807+
```csharp
808+
services.AddSingleton<IChatStore, PostgresChatStore>();
809+
```
810+
811+
Both implementations utilize indexed queries with result limits to ensure consistent performance even as your chat history grows. The partitioned approach in PostgreSQL offers additional benefits for long-term data retention and archival strategies.
812+
813+
## Admin UI Analytics
814+
815+
Once chat history persistence is enabled, the Admin UI provides comprehensive analytics dashboards that deliver actionable insights into your AI infrastructure. The analytics interface offers multiple views to help you understand costs, optimize token usage, and monitor activity patterns across all configured AI providers and models.
816+
817+
The analytics dashboard includes three primary tabs:
818+
819+
- **Cost Analysis** - Track spending across providers and models with daily and monthly breakdowns
820+
- **Token Usage** - Monitor input and output token consumption to identify optimization opportunities
821+
- **Activity** - Review detailed request logs with full conversation history and metadata
822+
823+
These visualizations enable data-driven decisions about provider selection, model usage, and cost optimization strategies.
824+
825+
### Cost Analysis
826+
827+
The Cost Analysis tab provides financial visibility into your AI operations with interactive visualizations showing spending distribution across providers and models. Daily cost trends help identify usage spikes, while monthly aggregations reveal long-term patterns. Pie charts break down costs by individual models and providers, making it easy to identify your most expensive AI resources and opportunities for cost optimization.
828+
829+
![](https://servicestack.net/img/posts/ai-chat-analytics/admin-chat-costs.webp)
830+
831+
### Token Usage
832+
833+
The Token Usage tab tracks both input (prompt) and output (completion) tokens across all requests. Daily usage charts display token consumption trends over time, while model and provider breakdowns show which AI resources consume the most tokens. This granular visibility helps optimize prompt engineering, identify inefficient usage patterns, and forecast capacity requirements.
834+
835+
![](https://servicestack.net/img/posts/ai-chat-analytics/admin-chat-tokens.webp)
836+
837+
### Activity Log
838+
839+
The Activity tab maintains a searchable log of all AI chat requests, displaying timestamps, models, providers, and associated costs. Clicking any request opens a detailed view showing the complete conversation including user prompts, AI responses, token counts, duration, and the full request payload. This audit trail is invaluable for debugging, quality assurance, and understanding how your AI features are being used in production.
840+
841+
![](https://servicestack.net/img/posts/ai-chat-analytics/admin-chat-activity.webp)

0 commit comments

Comments
 (0)