Skip to content

Chat for your app's users ​

Your application can offer its own end users an AI assistant that answers from your knowledge base. Your server sends each message to TenancyEngine, which calls the model and returns the reply. You never hold an AI provider key.

Chat uses an app chat key, not an organization API key. The chat key is tied to one application, so an app that only chats never holds a key that can manage anything. Issue chat keys under Knowledge in the application workspace. Chat needs a plan that includes AI chat; without one the call fails with 402 ai_chat_not_included.

Send a message from .NET ​

csharp
var chat = new TenancyEngineAppChatClient(httpClient, chatKey); // httpClient.BaseAddress = the platform API origin

// One complete reply
AppChatResult result = await chat.ChatAsync(tenantId, "How do I reset my password?", history);
Console.WriteLine(result.Reply);

// Or stream it as it is written
await foreach (var ev in chat.ChatStreamAsync(tenantId, "How do I reset my password?", history))
{
    if (ev.Kind == AppChatStreamEventKind.Delta) Console.Write(ev.TextDelta);
    else Console.WriteLine($"\n{ev.Final!.InputTokens} in, {ev.Final.OutputTokens} out");
}
  • tenantId is the customer (tenant) of your application the conversation belongs to.
  • history is the earlier turns, oldest first, each with role user or assistant. Any other role is rejected. Very long histories are trimmed from the oldest end.
  • The result carries the reply, the knowledge-base documents it drew on (Sources) and the token counts.
  • context (optional) is a short block of reference text for this one question, such as the plan the visitor is looking at or a small public price list. See Reference context.

Reference context ​

Use context when the answer lives in your page or your database rather than in uploaded documents.

csharp
var plan = "Pro plan: $20 a month, 5 seats, email support.";
var result = await chat.ChatAsync(tenantId, "Is support included?", history, context: plan);
  • Up to 6,000 characters. More than that, or a message and context that together exceed the input limit, returns a 400 with a message that names the limit. The oldest history turns are dropped first to make room.
  • It is treated as reference material only. Anything inside it that reads like an instruction is ignored, and text that tries to imitate the platform's own markers is neutralised. A context that looks like a deliberate attempt to take over the assistant is refused with the same 400 as a message would be.
  • It is used for this request only. It is not stored, not added to your knowledge base and not shown to other tenants or applications.
  • It counts toward the size of the request, so it is part of what each reply costs.
  • Do not put one customer's data in a request made on behalf of another. The platform scopes the conversation to the tenant you pass; it cannot know what you pasted.

The HTTP routes are POST /api/v1/apps/chat and POST /api/v1/apps/chat/stream with the header X-App-Chat-Key. The SDK sets it for you and does not modify your HttpClient, so one pooled client can serve several chat clients.

Errors ​

StatusSDK exceptionMeaning
400TenancyEngineValidationExceptionInvalid message, bad history role, or a message blocked as a prompt-injection attempt
401TenancyEngineAuthenticationExceptionMissing or revoked chat key
402TenancyEnginePlanRequiredExceptionPlan does not include chat, or the monthly allowance is used up
429TenancyEngineRateLimitedExceptionToo many messages (see below)
200, then an error eventTenancyEngineChatStreamExceptionA streaming reply failed after it had started

Rating answers ​

Show a thumbs up and a thumbs down under each reply. Every successful reply carries a FeedbackToken; pass it back with the rating.

csharp
AppChatResult reply = await chat.ChatAsync(tenantId, "Can I get a refund?");
// later, when the person clicks a thumb:
await chat.RateAnswerAsync(reply.FeedbackToken!, AppChatRating.Down, reason: "outdated");
  • reason is optional and only used with a thumbs down. Use one of wrong, outdated, unclear or off_topic. Anything else is ignored, so free text is never stored.
  • The token is good for 24 hours and ties the rating to that reply. Rating the same reply again changes the earlier rating.
  • Only the rating, the reason and the documents behind the reply are kept. Do not send the person's message.
  • Ratings have their own allowance, separate from chat messages: 30 a minute per end user and 300 a minute per chat key by default. Forward X-App-Chat-End-User here too.
  • A 400 with invalid_feedback_token means the token expired or does not belong to this chat key.

Your team sees the results on the application's Knowledge tab under How customers rate the answers.

Talk to a person ​

When the assistant cannot help, let the person hand the conversation to your team. Turn it on under Talk to a person on the application's Knowledge tab, then check it from your app before showing a button.

csharp
AppChatHandoffInfo info = await chat.GetHandoffInfoAsync();
if (info.Available)
{
    // Show info.OfficeHoursMessage, the address field, and the exact conversation that will be sent. Then:
    await chat.SendHandoffAsync("[email protected]", conversation, customerName: "Pat", lastFeedbackToken: reply.FeedbackToken);
}
  • The conversation is emailed to the address your team confirmed. The person's address is the Reply-To, so your team answers from their own inbox. Nothing is sent to the person.
  • Send only what the person saw: at most 20 messages, 8,000 characters each and 32,000 in total, each with role user or assistant. A longer or invalid conversation is rejected with a 400 and nothing is sent.
  • Nothing is stored by TenancyEngine. The conversation exists only in the email, which appears in the application's email delivery log.
  • lastFeedbackToken is optional. When you pass the last answer's token, what the assistant was searching for is added to your list of unanswered questions.
  • Limits protect your inbox: 3 hand-offs an hour per end user and 60 an hour per chat key by default. Forward X-App-Chat-End-User here too. Going over returns 429 with Retry-After.
  • A 404 with handoff_not_available means the feature is off or the address is not confirmed. Check GetHandoffInfoAsync first.

Message limits ​

Limits protect your AI spend. They are per minute, counted inside one chat key and one tenant:

LimitDefaultApplies to
Per end user20 messagesOne person, identified by the X-App-Chat-End-User header you forward, or by their IP address when you send none
Per tenant120 messagesAll end users of that tenant together, even if a client changes its end-user id
Per IP, before the key is checked600 requestsBlunts key guessing and floods. It is high on purpose so a server that proxies many visitors from one address is not starved

Forward the end-user id. If your backend calls chat on behalf of visitors, send each visitor's stable id (a user id or session id, not their email) in X-App-Chat-End-User. Without it, every visitor behind your server shares one IP-based allowance of 20 messages a minute.

When a limit is hit the response is 429 with a Retry-After header and a body like:

json
{ "error": "chat_rate_limited", "scope": "end_user", "limit": 20, "retryAfterSeconds": 12,
  "message": "You are sending messages too quickly. Please wait a moment and try again." }

scope is end_user or tenant. Show your user the message and retry after retryAfterSeconds. A tenant-level rejection does not use up the end user's allowance. If the shared limit store is unavailable, chat refuses requests rather than allow unmetered spend.

The defaults can be changed, and set per application, by TenancyEngine support (AppChat:RateLimit settings).

Cost ​

Each reply is charged to your AI credit balance, including the cached part of the prompt. See AI credits.

TenancyEngine platform documentation