C API reference · Kjarni 0.2.8

chat

kjarni_chat_config_default

struct KjarniChatConfig kjarni_chat_config_default(void);

Get default chat configuration.

kjarni_chat_new

enum KjarniErrorCode kjarni_chat_new(const struct KjarniChatConfig *config, struct KjarniChat **out);

Create a new Chat instance.

Safety

  • config must be valid or NULL (uses defaults)
  • out must be a valid pointer
  • The returned handle must be freed with kjarni_chat_free

kjarni_chat_new_with_draft

enum KjarniErrorCode kjarni_chat_new_with_draft(const struct KjarniChatConfig *config, const char *draft_model_name, uintptr_t draft_tokens, struct KjarniChat **out);

Creates a chat handle with speculative decoding enabled.

A draft model proposes draft_tokens tokens per round and this model verifies them in a single pass. Decode is bandwidth bound, so reading the target's weights once for several tokens rather than once per token is the whole saving: measured on Qwen2.5, 0.5B drafting for 1.5B runs 3.5x faster at eight proposed tokens. The draft must share the target's vocabulary, which in practice means a smaller model of the same family.

A separate entry point rather than two more fields on KjarniChatConfig: that struct is returned by value from kjarni_chat_config_default and passed back by pointer, so growing it would make every already-compiled caller hand over a shorter allocation than this library reads. Adding a function cannot break a binary that never calls it.

Pass NULL for draft_model_name to get exactly kjarni_chat_new.

Safety config must be null or a valid KjarniChatConfig. draft_model_name must be null or a valid NUL-terminated string. out must be a valid pointer.

kjarni_chat_free

void kjarni_chat_free(struct KjarniChat *chat);

Free a Chat instance.

kjarni_chat_send

enum KjarniErrorCode kjarni_chat_send(struct KjarniChat *chat, const char *message, const struct KjarniGenerationConfig *gen_config, char **out);

Send a message and get the full response.

Safety

  • chat must be a valid handle from kjarni_chat_new
  • message must be a valid UTF-8 C string
  • gen_config may be NULL (uses defaults)
  • out must be a valid pointer; caller must free result with kjarni_string_free

kjarni_chat_stream

enum KjarniErrorCode kjarni_chat_stream(struct KjarniChat *chat, const char *message, const struct KjarniGenerationConfig *gen_config, KjarniStreamCallbackFn callback, void *user_data, const struct KjarniCancelToken *cancel_token);

Stream a response token by token via callback.

The callback receives each token's text as a C string. Return false from the callback to stop generation early.

Safety

  • chat must be a valid handle
  • message must be valid UTF-8
  • callback must be a valid function pointer
  • cancel_token may be NULL

kjarni_chat_send_with_history

enum KjarniErrorCode kjarni_chat_send_with_history(struct KjarniChat *chat, const int32_t *roles, const char *const *contents, uintptr_t history_len, const char *message, const struct KjarniGenerationConfig *gen_config, char **out);

Send a message with explicit history. Does not modify the history

kjarni_chat_stream_with_history

enum KjarniErrorCode kjarni_chat_stream_with_history(struct KjarniChat *chat, const int32_t *roles, const char *const *contents, uintptr_t history_len, const char *message, const struct KjarniGenerationConfig *gen_config, KjarniStreamCallbackFn callback, void *user_data, const struct KjarniCancelToken *cancel_token);

Stream a response with explicit history. Does not modify the history.

The counterpart to kjarni_chat_send_with_history. IChatClient and other stateless APIs hand back the whole transcript on every call, so streaming has to accept a history the same way sending does; without this, a streaming caller could only ever produce single-turn replies.

Roles follow the same encoding as kjarni_chat_send_with_history: 0 = system, 1 = user, 2 = assistant.

kjarni_chat_conversation_new

enum KjarniErrorCode kjarni_chat_conversation_new(struct KjarniChat *chat, struct KjarniChatConversation **out);

Create a new stateful conversation from a Chat instance

kjarni_chat_conversation_free

void kjarni_chat_conversation_free(struct KjarniChatConversation *convo);

Free a ChatConversation.

kjarni_chat_conversation_send

enum KjarniErrorCode kjarni_chat_conversation_send(struct KjarniChatConversation *convo, const char *message, const struct KjarniGenerationConfig *gen_config, char **out);

Send a message in a conversation and get the response

kjarni_chat_conversation_stream

enum KjarniErrorCode kjarni_chat_conversation_stream(struct KjarniChatConversation *convo, const char *message, const struct KjarniGenerationConfig *gen_config, KjarniStreamCallbackFn callback, void *user_data, const struct KjarniCancelToken *cancel_token);

Stream a response in a conversation.

The user message is added to history before streaming. After streaming completes, the full response is added to history.

Safety

  • Same requirements as kjarni_chat_stream
  • Parent Chat must still be alive

kjarni_chat_conversation_len

uintptr_t kjarni_chat_conversation_len(const struct KjarniChatConversation *convo);

Get the number of messages in the conversation history.

kjarni_chat_conversation_clear

void kjarni_chat_conversation_clear(struct KjarniChatConversation *convo, int32_t keep_system);

Clear the conversation history.

If keep_system is non-zero, the system prompt is preserved.

kjarni_chat_model_name

uintptr_t kjarni_chat_model_name(const struct KjarniChat *chat, char *buf, uintptr_t buf_len);

Get the model name. Returns number of bytes written (excluding null).

If buf is NULL or buf_len is 0, returns the required buffer size.

kjarni_chat_context_size

uintptr_t kjarni_chat_context_size(const struct KjarniChat *chat);

Get the context window size.

Kjarni runs the same engine on every platform: