Here's some quick xml to give you an idea:
<GameModifier>
<ModType>Resource</ModType>
<Attribute>Mana</Attribute>
<ApplyToCaster>1</ApplyToCaster>
<Calculate InternalName="Value" ValueOwner="TargetUnit">
<Expression><![CDATA[[Unit_GetLevel] * 5]]></Expression>
</Calculate>
</GameModifier>
<GameModifier>
<ModType>Resource</ModType>
<Attribute>Mana</Attribute>
<ApplyToCaster>1</ApplyToCaster>
<IsForSpellResist>1</IsForSpellResist>
<Calculate InternalName="Value" ValueOwner="TargetUnit">
<Expression><![CDATA[[Unit_GetLevel] * 10]]></Expression>
</Calculate>
</GameModifier>
If you put both these into a spelldef, it will give the caster Target's level * 5 in mana if the spell hits, and Target's level * 10 in mana if it gets resisted. Negative values would drain the resource instead. Keep in mind this can take you below 0 in a resource.
The point here is that you can make the spell cost dynamically AFTER it has been cast, and also based on if the spell is resisted or not.
For example: Recruit bandit: 10 gildar to cast the spell. If the spell is successful, it costs you another bandit's level * 5 gildar. If the spell isn't successful, it costs you bandit's level * 10 gildar. He was just stringing you along to make you raise the bid, then betrayed you.
Or you could turn it around, make the cost higher if you succeed, but much lower if you fail the takeover. You only have to pay him if he agrees to join you, after all.