Plugin Examples

June 11, 2026 · View on GitHub

Educational plugin examples demonstrating the KohakuTerrarium plugin API. Each file is a standalone, runnable plugin that covers specific hook types.

Quick Start

Add any plugin to your creature's config.yaml:

plugins:
  - name: hello
    type: custom
    module: examples.plugins.hello_plugin
    class: HelloPlugin

Examples

FileHooks DemonstratedDifficulty
hello_plugin.pyon_load, on_agent_start, on_agent_stopBeginner
tool_timer.pypre_tool_execute, post_tool_execute, state persistenceBeginner
tool_guard.pypre_tool_execute, PluginBlockErrorIntermediate
prompt_injector.pypre_llm_call (message modification)Intermediate
response_logger.pypost_llm_call, on_event, on_interrupt, on_compact_endIntermediate
budget_enforcer.pypost_llm_call, pre_llm_call (blocking), state persistenceAdvanced
subagent_tracker.pypre_subagent_run, post_subagent_run, on_task_promotedAdvanced
webhook_notifier.pyAll callbacks, inject_event, switch_modelAdvanced

Plugin Hook Reference

Pre/Post Hooks (pipeline: can transform or block)

  • pre_llm_call(messages, **kw) → modified messages or None
  • post_llm_call(messages, response, usage, **kw) → observation only
  • pre_tool_execute(args, **kw) → modified args or None (raise PluginBlockError to block)
  • post_tool_execute(result, **kw) → modified result or None
  • pre_subagent_run(task, **kw) → modified task or None (raise PluginBlockError to block)
  • post_subagent_run(result, **kw) → modified result or None

Callbacks (fire-and-forget: observe only)

  • on_agent_start() / on_agent_stop()
  • on_event(event): every incoming trigger event
  • on_interrupt(): user pressed Escape
  • on_task_promoted(job_id, tool_name): direct task → background
  • on_compact_start(context_length) / on_compact_end(summary, messages_removed)

PluginContext Methods

  • get_state(key) / set_state(key, value): persist plugin data in session
  • inject_event(event): push a TriggerEvent into the agent's queue
  • switch_model(name): change the LLM model at runtime