AWS Bedrock with .NET: Conversational AI Converse API
What is the AWS Bedrock Converse API and Why Use It?
As generative AI becomes more integrated into business workflows, the need for natural, multi-turn conversations with AI models is growing. AWS Bedrock's Converse API is designed to simplify these interactions, enabling developers to build chatbots, virtual agents, and assistants that can maintain context and handle complex dialogues.
Unlike the traditional Invoke API, which is optimized for single-turn prompts, the Converse API is purpose-built for multi-turn, stateful conversations. This makes it ideal for scenarios where the AI needs to remember previous exchanges and provide contextually relevant responses.
What is the AWS Bedrock Converse API?
The Converse API is AWS Bedrock’s answer to the growing demand for natural, multi-turn, context-aware conversations with generative AI models. As businesses and developers began integrating AI into customer support, virtual assistants, and workflow automation, it became clear that single-turn, stateless APIs (like the original Invoke API) were not enough. Users expect AI to remember context, follow up on previous questions, and provide a seamless, human-like experience.
The Converse API was introduced to fill this gap. It allows developers to send the entire conversation history to the model, enabling the AI to generate responses that are aware of everything said so far. This is a major step forward for building chatbots, support agents, and assistants that feel truly interactive and intelligent.
Why: The Need for the Converse API
- Context Retention: Converse API allows the model to maintain conversation history, enabling more natural and coherent interactions.
- Multi-Turn Dialogues: Essential for customer support, virtual assistants, and any use case where back-and-forth is required.
- Seamless Integration: The API is designed to work smoothly with AWS SDKs, making it easy to add to your .NET applications.
In short, if your application needs to "remember" what was said earlier and respond accordingly, the Converse API is the right tool.
Why Did AWS Create the Converse API?
- Context Retention: Real conversations require memory. Converse API lets you pass the full message history, so the model can reference earlier turns and maintain continuity.
- Multi-Turn Dialogues: Many business scenarios (support, sales, onboarding) require back-and-forth exchanges, not just one-off answers.
- Better User Experience: Users expect AI to "remember" what they said. Stateless APIs break this illusion and frustrate users.
- Enterprise Integration: Converse API is built for production, with IAM, monitoring, and cost controls.
In summary, Converse API exists because real-world AI needs to be conversational, not just responsive.
When to Use Converse API: Use Case Scenarios
- AI-powered customer support agents that handle multi-step troubleshooting.
- Virtual assistants that help users complete tasks over several messages.
- Educational bots that guide users through learning modules.
- Any scenario where context and continuity are critical for user experience.
When and Where to Use the Converse API
- Customer Support: Multi-step troubleshooting, order tracking, and escalation flows.
- Virtual Assistants: Scheduling, reminders, and task management that require context.
- Education: Tutoring bots that guide users through lessons and remember progress.
- Healthcare: Symptom checkers and patient intake bots that need to remember previous answers.
- Internal Tools: HR, IT, and operations bots that help employees with ongoing requests.
- Any scenario where context, continuity, and memory are essential for a good user experience.
How Does Converse API Compare to Invoke API?
- Invoke API: Best for single-turn, stateless prompts. Each request is independent, and the model does not retain any memory of previous interactions.
- Converse API: Designed for multi-turn, stateful conversations. The model receives the full conversation history and can generate responses that reference earlier messages.
If you need chat-like, context-aware interactions, use Converse. For simple, one-off completions, use Invoke.
Converse API vs. Invoke API: Technical and Practical Differences
- Invoke API: Stateless. Each request is a single prompt/response. No memory of previous turns. Simpler, but limited for conversations.
- Converse API: Stateful. You send the full conversation history (as a list of messages). The model can reference, summarize, or clarify based on prior turns.
Technical Note: With Converse, you are responsible for maintaining and sending the conversation history. This gives you control, but also means you must manage message length and context window limits.
- When to use Invoke: Single-shot Q&A, summarization, or content generation.
- When to use Converse: Chatbots, support agents, assistants, and any workflow with back-and-forth dialogue.
How: Building an AI Customer Service Agent with Converse API
1. Prerequisites
- AWS account with Bedrock access enabled.
- AWS credentials with permissions for Bedrock Converse API.
- .NET 8 or later SDK installed.
- NuGet package: AWSSDK.BedrockRuntime
2. Service Implementation Example
Below is a simplified version of a customer support agent using the Bedrock Converse API in C#. The agent maintains conversation history and provides context-aware responses.
using Amazon.BedrockRuntime;
using Amazon.BedrockRuntime.Model;
using System.Collections.Generic;
using System.Threading.Tasks;
public class BedrockCustomerSupportService
{
private readonly AmazonBedrockRuntimeClient _client;
private const string ModelId = "anthropic.claude-3-haiku-20240307-v1:0";
public BedrockCustomerSupportService()
{
_client = new AmazonBedrockRuntimeClient(Amazon.RegionEndpoint.USEast1);
}
/// <summary>
/// Simulates an AI customer support staff in a multi-turn conversation using the Bedrock Converse API (no tools).
/// </summary>
/// <param name="conversationHistory">The full conversation history as a list of Message objects (user and assistant turns).</param>
/// <returns>AI-generated customer support response as plain text.</returns>
public async Task<string> GetSupportResponse(List<Message> conversationHistory)
{
var converseRequest = new ConverseRequest
{
System = new List<SystemContentBlock>
{
new SystemContentBlock
{
Text = "You are an AI customer support staff. Be polite, helpful, and concise. Address the customer's issue and provide clear next steps or solutions. Maintain context across the conversation."
}
},
ModelId = ModelId,
Messages = conversationHistory
};
var response = await _client.ConverseAsync(converseRequest);
var responseText = string.Empty;
foreach (var content in response.Output.Message.Content)
{
if (content.Text != null)
{
responseText += content.Text;
}
}
return responseText;
}
}
3. How the Example Works
- System Prompt: Sets the behavior and tone for the AI agent.
- Model Selection: Uses Anthropic Claude 3 Haiku for fast, cost-effective responses.
- Conversation History: Passes the entire message history to maintain context.
- Response Handling: Aggregates the AI's response from the output content blocks.
4. Example Use Case: AI Customer Service Agent
Suppose a user is troubleshooting an issue with their order. The conversation might look like this:
// Example conversation history
var conversationHistory = new List<Message>
{
new Message { Role = "user", Content = new List<ContentBlock> { new ContentBlock { Text = "My order hasn't arrived yet." } } },
new Message { Role = "assistant", Content = new List<ContentBlock> { new ContentBlock { Text = "I'm sorry to hear that. Can you provide your order number?" } } },
new Message { Role = "user", Content = new List<ContentBlock> { new ContentBlock { Text = "It's 12345." } } }
};
var service = new BedrockCustomerSupportService();
string response = await service.GetSupportResponse(conversationHistory);
Console.WriteLine(response);
The AI will respond with a context-aware message, such as confirming the order status or providing next steps.
6. Demo: Interactive Customer Support Chat
Below is a complete demo of a console-based customer support chat using the Bedrock Converse API. This example shows how to maintain the conversation loop, collect user input, and keep the conversation history updated for each turn.
// Customer support chat demo
async Task RunBedrockCustomerSupportChat()
{
try
{
var supportService = new BedrockCustomerSupportService();
var conversation = new List<Amazon.BedrockRuntime.Model.Message>();
Console.WriteLine("\nWelcome to AI Customer Support! Type 'Exit Chat' to end the conversation.\n");
while (true)
{
Console.Write("You: ");
var userInput = Console.ReadLine();
if (string.Equals(userInput, "Exit Chat", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("\nThank you for chatting with AI Customer Support. Goodbye!\n");
break;
}
// Add user message to conversation
conversation.Add(new Amazon.BedrockRuntime.Model.Message
{
Role = "user",
Content = new List<Amazon.BedrockRuntime.Model.ContentBlock>
{
new Amazon.BedrockRuntime.Model.ContentBlock { Text = userInput }
}
});
// Get AI response
var response = await supportService.GetSupportResponse(conversation);
// Add assistant response to conversation
conversation.Add(new Amazon.BedrockRuntime.Model.Message
{
Role = "assistant",
Content = new List<Amazon.BedrockRuntime.Model.ContentBlock>
{
new Amazon.BedrockRuntime.Model.ContentBlock { Text = response }
}
});
Console.WriteLine($"AI: {response}\n");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine(ex.StackTrace);
}
}
This interactive loop demonstrates how to build a real-time, multi-turn customer support chat using the Converse API. The conversation history is updated after every user and AI message, ensuring context is preserved throughout the session.
Summary
- What: The Bedrock Converse API enables multi-turn, context-aware conversations with generative AI models.
- When: Use it for chatbots, customer support, and any scenario requiring context retention.
- How: Integrate with AWS SDK for .NET and pass conversation history for seamless, intelligent interactions.
For more details and the full code, see the GitHub example.
References & Further Reading
For reference, the code repository being discussed is available at github: https://github.com/ajaysskumar/ai-playground/blob/main/AwsBedrockExamples/Services/BedrockCustomerSupportService.cs
Thanks for reading through. Please share feedback, if any, in comments or on my email ajay.a338@gmail.com
