Skip to content

Stream "API calls quota exceeded" #112

Description

@JadynWong

The streaming chat frequently triggers the API quota exceeded error. Any suggestions for optimisation?

2025-09-04 07:40:10.680 +00:00 [ERR] System.Exception: Exception of type 'Microsoft.Teams.Common.Http.HttpException' was thrown.
 ---> "API calls quota exceeded"
   --- End of inner exception stack trace ---
   at Microsoft.Teams.Common.Extensions.TaskExtensions.Retry[T](Task`1 task, Int32 max, Int32 delay)
   at Microsoft.Teams.Common.Extensions.TaskExtensions.Retry[T](Task`1 task, Int32 max, Int32 delay)
   at Microsoft.Teams.Common.Extensions.TaskExtensions.Retry[T](Task`1 task, Int32 max, Int32 delay)
   at Microsoft.Teams.Common.Extensions.TaskExtensions.Retry[T](Task`1 task, Int32 max, Int32 delay)
   at Microsoft.Teams.Plugins.AspNetCore.AspNetCorePlugin.Stream.Close()
   at Microsoft.Teams.Apps.App.Process(ISenderPlugin sender, ActivityEvent event, CancellationToken cancellationToken)
[Message]
public async Task OnMessage(IContext<MessageActivity> context, [Context] MessageActivity activity, [Context] IContext.Client client)
{
  var state = State.From(context);

  using var scope = _serviceScopeFactory.CreateScope();
  
  var chatPrompt = scope.ServiceProvider.GetRequiredService<OpenAIChatPrompt>();

  var contents = new List<IContent>();
  if (!string.IsNullOrWhiteSpace(context.Activity.Text))
  {
      contents.Add(new TextContent() { Text = context.Activity.Text });
  }
  if (context.Activity.Attachments?.Count > 0)
  {
      _logger.LogInformation("Attachment count: {Count}", context.Activity.Attachments.Count);
      await ProcessAttachments(context, contents);
  }

  var response = await chatPrompt.Send(
      content: [.. contents],
      options: new() { Messages = state.Messages },
      onChunk: (chunk) => Task.Run(() =>
      {
         context.Stream.Emit(chunk);
      }, context.CancellationToken),
      cancellationToken: context.CancellationToken
  );
  
  var activityWithResponse = new MessageActivity();
  activityWithResponse.AddAIGenerated();
  context.Stream.Emit(activityWithResponse);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions