Docs/API/API

    Streaming

    Last updated · MAR 2026·Read as Markdown

    Enable streaming by setting stream: true in your request.

    json
    {
      "model": "myagent-123",
      "messages": [{"role": "user", "content": "Hello!"}],
      "stream": true
    }

    Server-sent events with JSON chunks:

    code
    data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1699451234,"model":"myagent-123","choices":[{"index":0,"delta":{"content":"Hello"},"finish_reason":null}]}
    
    data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1699451234,"model":"myagent-123","choices":[{"index":0,"delta":{"content":" there!"},"finish_reason":null}]}
    
    data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1699451234,"model":"myagent-123","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
    
    data: [DONE]
    javascript
    async function streamChat(model, messages) {
      const response = await fetch('https://app.avcodex.com/api/v1/chat/completions', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${process.env.AVCODEX_API_KEY}`
        },
        body: JSON.stringify({
          model,
          messages,
          stream: true
        })
      });
    
      const reader = response.body.getReader();
      const decoder = new TextDecoder();
    
      while (true) {
        const { done, value } = await reader.read();
        if (done) break;
    
        const chunk = decoder.decode(value);
        const lines = chunk.split('\n');
    
        for (const line of lines) {
          if (line.startsWith('data: ')) {
            const data = line.slice(6);
            if (data === '[DONE]') return;
    
            try {
              const parsed = JSON.parse(data);
              const content = parsed.choices[0]?.delta?.content || '';
              process.stdout.write(content);
            } catch (e) {
              // Skip parsing errors
            }
          }
        }
      }
    }
    python
    import requests
    import json
    import os
    
    response = requests.post(
        'https://app.avcodex.com/api/v1/chat/completions',
        headers={
            'Content-Type': 'application/json',
            'Authorization': f'Bearer {os.environ["AVCODEX_API_KEY"]}'
        },
        json={
            'model': 'myagent-123',
            'messages': [{'role': 'user', 'content': 'Hello!'}],
            'stream': True
        },
        stream=True
    )
    
    for line in response.iter_lines():
        if line:
            line = line.decode('utf-8')
            if line.startswith('data: '):
                data = line[6:]
                if data == '[DONE]':
                    break
    
                try:
                    chunk = json.loads(data)
                    content = chunk['choices'][0]['delta'].get('content', '')
                    print(content, end='', flush=True)
                except:
                    pass
    javascript
    import OpenAI from 'openai';
    
    const openai = new OpenAI({
      apiKey: process.env.AVCODEX_API_KEY,
      baseURL: 'https://app.avcodex.com/api/v1'
    });
    
    const stream = await openai.chat.completions.create({
      model: 'myagent-123',
      messages: [{ role: 'user', content: 'Hello!' }],
      stream: true
    });
    
    for await (const chunk of stream) {
      process.stdout.write(chunk.choices[0]?.delta?.content || '');
    }

    Each chunk contains:

    • id: same for every chunk in a response.
    • object: always "chat.completion.chunk".
    • created: Unix timestamp.
    • model: your agent name.
    • choices[0].delta: content increment.
    • choices[0].finish_reason: null until the last chunk.

    *AVCodex · Your AV expertise. Amplified by AI.*

    Was this helpful?
    Edit this page →