Skip to main content
Compared to traditional models, large language models let you anchor personas with custom prompts and pass conversation context, making multi-turn dialogs straightforward. Stepfun’s Chat Completion API accepts an array as the message list. Simply store user and assistant messages in order and send them back to the model to enable multi-turn conversations.

Implementing multi-turn chat

Persist conversations and messages in your system to build the conversation history.
  • Create a Chat when the user starts a conversation.
  • When the user sends a message, create a Message; fetch Messages in chronological order and pass them to the model so it can generate with context.
  • When the model responds, create another Message.

Conversation flow

Code example

Managing conversation context

Stepfun models support up to 256k context, so you can pass the full history. Context counts toward token usage, so choose a sensible history length for your scenario.
  • For casual chat, keep roughly 10 turns.
  • For deep, high-context scenarios, pass the full conversation to the model.

FAQ

  • To achieve correct multi-turn behavior, store timestamps and supply messages to the model in chronological order so it can understand the context properly.