• Stefy Lanza (nextime / spora )'s avatar
    engine: render chat prompt with a plain (cached) Jinja env — fixes ~40s per request · 080ecff7
    Stefy Lanza (nextime / spora ) authored
    Root cause (from [timing] logs): chat "build" = 39.6s while prefill+first-token =
    0.31s. The ~40s is create_chat_completion rendering the chat template via
    llama-cpp-python's ImmutableSandboxedEnvironment, whose per-operation security
    checks make a heavy template (gemma-4's recursive / O(n^2) macros over a big
    Kilocode conversation) take tens of seconds — every request.
    
    Fix (generic for ALL gguf/llama.cpp models): render the prompt ourselves with a
    plain jinja2.Environment compiled ONCE from the model's tokenizer.chat_template
    (same trim/lstrip, IgnoreGenerationTags + loopcontrols extensions, tojson filter,
    same render vars), then tokenize with add_bos=False + special=True (matching
    llama-cpp's added_special handling — no double BOS) and generate via
    create_completion. Output is byte-identical to the sandboxed render (verified), but
    without the sandbox overhead. Plus a small per-message render cache and finer
    timing ([timing] chat render / tokenize / create_completion setup / first-token).
    Falls back to create_chat_completion if the model has no chat_template or rendering
    fails.
    Co-Authored-By: 's avatarClaude Opus 4.8 <noreply@anthropic.com>
    Claude-Session: https://claude.ai/code/session_011DDv7BchtZQWsnPG6Jm49m
    080ecff7
Name
Last commit
Last update
codai Loading commit data...
docs Loading commit data...
packaging Loading commit data...
samples Loading commit data...
tests Loading commit data...
tools Loading commit data...
.dockerignore Loading commit data...
.gitignore Loading commit data...
AI.PROMPT Loading commit data...
CODERAI_API_DOCUMENTATION.md Loading commit data...
CoderAI.gif Loading commit data...
DISTRIBUTION.md Loading commit data...
LICENSE.md Loading commit data...
MULTIMODAL_CAPABILITIES.md Loading commit data...
MULTIMODAL_UI_EXAMPLES.md Loading commit data...
README.md Loading commit data...
build-oci.sh Loading commit data...
build.ps1 Loading commit data...
build.sh Loading commit data...
coderai Loading commit data...
coderai-broker-implementation-reference.md Loading commit data...
coderai-integration.md Loading commit data...
commands Loading commit data...
osxbuild.sh Loading commit data...
package-oci.sh Loading commit data...
package-tarball.sh Loading commit data...
requirements-nvidia.txt Loading commit data...
requirements-vulkan.txt Loading commit data...
requirements.txt Loading commit data...
run-oci.sh Loading commit data...
smoke-test-oci.sh Loading commit data...
todo.md Loading commit data...
video_editor.config.json Loading commit data...