File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -144,8 +144,12 @@ void Llama::reset() {
144144}
145145
146146int Llama::max_tool_result_size () {
147- // ~3 bytes/tok, 25% of ctx
148- return (llama_n_ctx (_ctx) / 4 ) * 3 ;
147+ // 75% of space available
148+ llama_memory_t mem = llama_get_memory (_ctx);
149+ llama_pos pos_max = llama_memory_seq_pos_max (mem, 0 );
150+ int n_ctx = llama_n_ctx (_ctx);
151+ int space_available = n_ctx - (pos_max + 1 );
152+ return std::max (256 , (space_available * 3 ) / 4 );
149153}
150154
151155bool Llama::is_memory_flush () {
@@ -388,6 +392,14 @@ string Llama::all(LlamaIter &iter) {
388392 return out;
389393}
390394
395+ float Llama::memory_kv_percent () {
396+ llama_memory_t mem = llama_get_memory (_ctx);
397+ llama_pos pos_max = llama_memory_seq_pos_max (mem, 0 );
398+ int n_ctx = llama_n_ctx (_ctx);
399+ int kv_used = (pos_max < 0 ) ? 0 : (int )pos_max + 1 ;
400+ return 100 .0f * kv_used / n_ctx;
401+ }
402+
391403LlamaMemoryInfo Llama::memory_info () {
392404 LlamaMemoryInfo info = {};
393405
Original file line number Diff line number Diff line change @@ -103,6 +103,7 @@ struct Llama {
103103
104104 // memory info
105105 LlamaMemoryInfo memory_info ();
106+ float memory_kv_percent ();
106107
107108 // creates an embedding vector of the given dimension for the given text
108109 bool embed_text (const std::string &text, std::vector<float > &out, int embed_dim);
Original file line number Diff line number Diff line change @@ -1979,9 +1979,9 @@ float AgentState::tokens_per_sec() const {
19791979}
19801980
19811981std::string AgentState::memory_info_status () const {
1982- LlamaMemoryInfo m = llama->memory_info ();
1983- auto message = m. kv_percent > 75 ? " (Warning: Approaching limit)" : " " ;
1984- return std::format (" \n [INFO] KV Cache: {}%{}" , m. kv_percent , message);
1982+ float kv_percent = llama->memory_kv_percent ();
1983+ auto message = kv_percent > 75 ? " (Warning: Approaching limit)" : " " ;
1984+ return std::format (" \n [INFO] KV Cache: {}%{}" , kv_percent, message);
19851985}
19861986
19871987std::string AgentState::memory_info_text () const {
You can’t perform that action at this time.
0 commit comments